How to... Insert similar if not exists?!?

Discussion in 'PHP' started by jmansa, Oct 7, 2008.

  1. #1
    I want to be able to create a similar record if it doesnt exist with a new uid attached to it.

    Explanation:
    I have a db table with these fields:
    id (unique)
    lang_id
    name
    text

    I then have lets say 3 records like this:
    id lang_id name text
    1, 1, test1, "Somthing here"
    2, 1, test2, "Something else here"
    3, 1, test3, "Something totally different here"

    I then want to be able to create 3 new records with a new uid (2) and new text. The id is given automatic and the name should be copied into the new records! If a record exist with both the old a the new uid it should just go on to the next record.

    I guess it is depending on the "name" field.

    I have tryid with this but with no luck:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Translation</title>
    </head>
    
    <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-family:Lucida Sans; font-size:11px; color:#333333;">
      <tr>
        <td style="padding:5px; font-size:14px; text-transform:uppercase; border-bottom:1px solid #990000;">Translation: <b>English to Germain</b></td>
      </tr>
      <tr><td height="10"></td></tr>
      <tr>
        <td style="padding:5px; margin-top:10px;">
        
    <?
    
    mysql_connect('localhost', 'username', 'password');
    mysql_select_db('my_db');
    
    
    if(!empty($_POST)) {
    	mysql_query("INSERT INTO language_data 
    	(name, text, lang_id)
    	VALUES
    	('".$_POST['name']."','".$_POST['text']."', '2')");
    	echo "Saved<br>";
    }
    
    
    
    $langs = array();
    $langs[2] = array();
    $sql = mysql_query("SELECT * FROM language_data WHERE lang_id = '2'");
    while($row = mysql_fetch_array($sql)) {
    	$langs[$row['lang_id']][] = $row['name'];
    }
    
    $sql = mysql_query("SELECT * FROM language_data WHERE lang_id = '1'");
    while($row = mysql_fetch_array($sql)) {
    	if(!in_array($row['name'], $langs['1'])) {
    		$edit[] = $row;
    	}
    }
    
    echo "Still needs to be translated: ".count($edit);
    
    $slice = array_slice($edit, 0, 1);
    
    ?>    
        </td>
      </tr>
      <tr>
        <td>
        <form action="translate.php" method="post">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td style="padding:5px;"> Tagname: <b><?= $slice[0]['name'] ?> </b>
          <input type="hidden" name="tagname" value="<?= $slice[0]['name'] ?>" />
    </b></td>
      </tr>
      <tr>
        <td style="padding:5px;">&nbsp;</td>
      </tr>
      <tr>
        <td><table width="500" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="250" style="padding:5px; font-size:12px; text-transform:uppercase; font-weight:bold;">English</td>
            <td width="250" style="padding:5px; font-size:12px; text-transform:uppercase; font-weight:bold;">Germain</td>
          </tr>
          <tr>
            <td valign="top" style="padding:5px; background-color:#e5e5e5; border-right:1px dotted #333333;"><?= $slice[0]['text'] ?></td>
            <td valign="top" style="padding:5px; background-color:#e5e5e5;"><textarea name="string" style="width:250px;height:150px;">Insert translation here...</textarea></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><br /><input type="submit" value="Save"></td>
      </tr>
    </table>
          </form>  
        
       </td>
      </tr>
    </table>
    </body>
    </html>
    PHP:
    Can somebody please help...
     
    jmansa, Oct 7, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    INSERT INTO `table` (`value`, `value`)
    VALUES ('value1',
    'value2')
    ON DUPLICATE KEY UPDATE value1 = 'value1';

    Check mysql documentation on ON DUPLICATE KEY when using UNIQUE indexes!
     
    EricBruggema, Oct 8, 2008 IP