alcoholic1 Posted May 31, 2006 Share Posted May 31, 2006 Hey i need some help Refreshing my memory with some PHPi have a form and that has 2 input box's lets say to keep it simple Name,Age and a sumit buttonbasically when the submit button is hit i need it to check if the Name(Primary Key) exist if it dosent insert it and if it dose exist and update the infomation wondering if it will be kinda like below [code]<?phpmysql_connect("localhost", "root", "####") or die(mysql_error());mysql_select_db("test") or die(mysql_error());$result = mysql_query("SELECT Name FROM testDB where Name = 'alcoholic1' ") or die(mysql_error()); if <> result.EOF then $result = mysql_query("UPDATE testD SET age='22' WHERE age='21'") or die(mysql_error()); else mysql_query("INSERT INTO testD (name, age) VALUES('Sandy Smith', '21' ) ") or die(mysql_error()); end ifphp?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10856-update-if-exists-or-insert-if-doesn%E2%80%99t/ Share on other sites More sharing options...
poirot Posted May 31, 2006 Share Posted May 31, 2006 Something like this:[code]$query = mysql_query("SELECT * FROM table WHERE name = 'blah'");if (mysql_num_rows($query) == 0) { INSERT code here} else { UPDATE code here}[/code]Or you could use the REPLACE MySQL command:[a href=\"http://dev.mysql.com/doc/refman/5.0/en/replace.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/replace.html[/a] Quote Link to comment https://forums.phpfreaks.com/topic/10856-update-if-exists-or-insert-if-doesn%E2%80%99t/#findComment-40570 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.