Jump to content

ON DUPLICATE KEY UPDATE when i use "join values"


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 :)

The proper syntax for ON DUPLICATE KEY UPDATE is

ON DUPLICATE KEY UPDATE
    column=someValue,
    column2=someOtherValue
    ...

 

just like an update statement.  You need to rewrite your query in that format.

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.

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

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

 

OKay thanks you answer dome my job perfectly....

 

right after

if (isset($_POST['category'])) {

i made delete query

$sql2 = "DELETE FROM info_cats WHERE info_id = '$caturi'"; mysql_query($sql2);

and its works... thank you very much :)))!!!!!!

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.