Lucky2710 Posted July 31, 2010 Share Posted July 31, 2010 I need to insert data into a table if it doesn't exist or update if it does. Can someone give me an example of this code. im using mySQL 5.1.42 im using phpMyAdmin 3.1.o Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/ Share on other sites More sharing options...
Alex Posted July 31, 2010 Share Posted July 31, 2010 $result = mysql_query("SELECT 1 FROM table WHERE col='something'"); if(mysql_num_rows($result)) { // update } else { // insert } Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093689 Share on other sites More sharing options...
Pikachu2000 Posted July 31, 2010 Share Posted July 31, 2010 This may or may not be applicable to your situation. ON DUPLICATE KEY UPDATE Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093695 Share on other sites More sharing options...
Lucky2710 Posted July 31, 2010 Author Share Posted July 31, 2010 Alright i thought I had it figured out, but i guess i was wrong! (Like always) Ima be a little more specific this time... I need to say if certain information exists on a row, then update, if not then insert! Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093698 Share on other sites More sharing options...
Alex Posted July 31, 2010 Share Posted July 31, 2010 If not insert it into that row, or insert a new row? Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093701 Share on other sites More sharing options...
Lucky2710 Posted July 31, 2010 Author Share Posted July 31, 2010 I need each user to have their own row (i.e "insert"), but the if they choose to update it, then i need it to update (i.e "update") i need it to do it at the same time so that the same script does both scenarios Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093702 Share on other sites More sharing options...
Lucky2710 Posted July 31, 2010 Author Share Posted July 31, 2010 each user needs to insert their own row and update that row. I hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093703 Share on other sites More sharing options...
Lucky2710 Posted July 31, 2010 Author Share Posted July 31, 2010 And the ON DUPLICATE KEY isnt helping, but thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093704 Share on other sites More sharing options...
Alex Posted July 31, 2010 Share Posted July 31, 2010 I'm not entirely sure what you're asking, but I think what I posted in my first reply is what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093706 Share on other sites More sharing options...
Lucky2710 Posted July 31, 2010 Author Share Posted July 31, 2010 Not really. I need for when a user fills in a form. (I'm working on the process script) For the form to take that information and insert the users input into the mysql table. Each user will have his/her own row. If at any point the user wants to change the information i want them to just be able to fill in the form again and re submit it. All of the resubmitted info needs to override that users original input. So if not exist then insert, if exist then update. (I don't see how your first script does that, i see update if else insert but how ur script decides which to do doesn't appear to be what i need! Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093710 Share on other sites More sharing options...
Pikachu2000 Posted July 31, 2010 Share Posted July 31, 2010 I'm assuming all the users are registered already, and at least have a username, password, and primary key id in the database. If that's the case a regular UPDATE query should be all you need. If the field is empty, it will insert the data, if not, it will overwrite it. Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093711 Share on other sites More sharing options...
Lucky2710 Posted July 31, 2010 Author Share Posted July 31, 2010 The user data is in a separate table than this info is going into. Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093719 Share on other sites More sharing options...
jcbones Posted August 1, 2010 Share Posted August 1, 2010 Set a hidden field if the user is updating the information. In your process script, check for that hidden field, if it is there, update, if not insert. Quote Link to comment https://forums.phpfreaks.com/topic/209465-insert-if-not-exist-update-if-if-it-does/#findComment-1093751 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.