Jump to content

ON DUPLICATE KEY UPDATE when i use "join values"


lasha

Recommended Posts

Hello :)

 

Please, need some help in "ON DUPLICATE KEY UPDATE"

 

Code below does not do the thing :(

 

$sql = "INSERT INTO info_cats (cat_id, info_id) VALUES " . join(',', $values) . "ON DUPLICATE KEY UPDATE (cat_id, info_id) =" . join(',', $values);

 

 

Thanks in advice :)

Link to comment
Share on other sites

Not knowing much else of your code, this is how it would need to be done, as shown by kicken

 

$sql = "INSERT INTO info_cats (cat_id, info_id) VALUES (" . join(',', $values) . ") ON DUPLICATE KEY UPDATE cat_id = $values[0], info_id = $values[1]";

 

If you want something different, you will need to elaborate more on what you are looking for and possibly show more code.

Link to comment
Share on other sites

Okey is't my code.

 

and see attachment...

there is categories, when i select categories it inserts good but may be there is already category selected in same id its editing...

i want not to have dublicates...

 

Thanks :)

 

 

<?php 
if (isset($name) && isset($sur) && isset($id))
{
$result = mysql_query ("UPDATE info SET name='$name', sur='$sur' WHERE id='$id'");

if ($result == 'true')
{
$caturi =  $id;
}
else {echo "<p>Not Updated! :|</p>";}
}		 
else 
{
echo "<p>Enter info completely...</p>";
}

if (isset($_POST['category'])) {
        
foreach ($_POST['category'] as $cat) {
$values[]  = sprintf ("(%d, '%s')", intval($cat), mysql_real_escape_string($caturi));
}
    $sql = "INSERT INTO info_cats (cat_id, info_id) VALUES " . join(',', $values);
    mysql_query($sql);
echo $sql;
}

?>

post-136069-13482403782205_thumb.jpg

Link to comment
Share on other sites

You should probably just be running a DELETE query first to remove existing categories, then insert the category list again.  Just doing an insert (even with the duplicate key part) won't remove a category if you were to unselect one that had been previously added

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.