violinrocker Posted March 31, 2011 Share Posted March 31, 2011 i dont know if i remember this right.... but months ago i found an article saying that UPDATE is nice since it can do INSERT if there is nothing to update... is this true? or is there other functions that can do this? Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/ Share on other sites More sharing options...
betterphp Posted March 31, 2011 Share Posted March 31, 2011 you can do it the other way around using ON DUPLICATE KEY UPDATE Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1195017 Share on other sites More sharing options...
violinrocker Posted March 31, 2011 Author Share Posted March 31, 2011 okay so im not familiar with ON DUPLICATE KEY UPDATE. i googled it and i dont know how to apply it. i saw one that says it should be like this INSERT INTO TABLE (column1, column2, column3) VALUES (value1, value2, value3) ON DUPLICATE KEY UPDATE SET column1=value1, column2=value2, column3=value3"; how can i make it to work like... insert into table "thisname" but if there is already a data with name="thisname" even though other fields do not match itll update them... or if it works vice versa... update then if there is none... insert. sorry im a noob, please be patient with me Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1195037 Share on other sites More sharing options...
betterphp Posted March 31, 2011 Share Posted March 31, 2011 it works the first way. It first tried to insert the row, then it if finds a duplicate key that is either primary or unique it will do the update part. Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1195059 Share on other sites More sharing options...
violinrocker Posted April 2, 2011 Author Share Posted April 2, 2011 whats wrong with this? $sql = "UPDATE `users` SET `css` = '$_POST[css]' WHERE `users`.`username` = '$session->username' LIMIT 1"; $sql2 = "INSERT INTO user_extension (username, sig) VALUES ($session->username, $_POST[sig]) ON DUPLICATE KEY UPDATE SET $username=$session->username, sig=$_POST[sig]"; if (!mysql_query($sql,$con)) {?>Attention!<? die('Error: ' . mysql_error()); ?><? } if (!mysql_query($sql2,$con)) {?>Attention!<? die('Error: ' . mysql_error()); ?><? }} ?>CSS & sig Success!<? } ?> ERROR Attention!Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET =Violinrocker, sig=janime' at line 3 Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1195814 Share on other sites More sharing options...
violinrocker Posted April 2, 2011 Author Share Posted April 2, 2011 oh w8.. i think i found what was wrong... sorry xD Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1195824 Share on other sites More sharing options...
violinrocker Posted April 2, 2011 Author Share Posted April 2, 2011 hmmm another error.... $sql2 = "INSERT INTO user_extension (username, sig) VALUES ($session->username, $_POST[sig]) ON DUPLICATE KEY UPDATE $_POST[sig]=sig"; $session->username=violinrocker Attention! SIG Error: Unknown column 'Violinrocker' in 'field list' Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1195828 Share on other sites More sharing options...
violinrocker Posted April 2, 2011 Author Share Posted April 2, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1195840 Share on other sites More sharing options...
betterphp Posted April 8, 2011 Share Posted April 8, 2011 bump can you not wait more than 2 hours !! Unknown column 'Violinrocker' in 'field list' means you have the wrong column name, simple as that. Quote Link to comment https://forums.phpfreaks.com/topic/232303-insert-if-there-is-nothing-to-update/#findComment-1198747 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.