lasha Posted September 13, 2012 Share Posted September 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268335-on-duplicate-key-update-when-i-use-join-values/ Share on other sites More sharing options...
kicken Posted September 13, 2012 Share Posted September 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268335-on-duplicate-key-update-when-i-use-join-values/#findComment-1377660 Share on other sites More sharing options...
lasha Posted September 13, 2012 Author Share Posted September 13, 2012 Thanks for reply, okey i understend that but how to make it when i have join(values)? Quote Link to comment https://forums.phpfreaks.com/topic/268335-on-duplicate-key-update-when-i-use-join-values/#findComment-1377661 Share on other sites More sharing options...
premiso Posted September 13, 2012 Share Posted September 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268335-on-duplicate-key-update-when-i-use-join-values/#findComment-1377664 Share on other sites More sharing options...
lasha Posted September 13, 2012 Author Share Posted September 13, 2012 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268335-on-duplicate-key-update-when-i-use-join-values/#findComment-1377673 Share on other sites More sharing options...
kicken Posted September 13, 2012 Share Posted September 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268335-on-duplicate-key-update-when-i-use-join-values/#findComment-1377679 Share on other sites More sharing options...
lasha Posted September 13, 2012 Author Share Posted September 13, 2012 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 ))!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/268335-on-duplicate-key-update-when-i-use-join-values/#findComment-1377691 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.