2tonejoe Posted July 31, 2007 Share Posted July 31, 2007 What is the proper way to check a table to see if something exists before inserting it? If it does exist, update the information, if it doesn't . . insert the info? This would be php into mySQL. . . . I have tried before and been unsuccessful .. . something like this if name='john' and week='22' - update row info ELSE insert row info anyone help me out? Quote Link to comment https://forums.phpfreaks.com/topic/62754-check-before-insert/ Share on other sites More sharing options...
almightyegg Posted July 31, 2007 Share Posted July 31, 2007 <?php $one = mysql_num_rows(mysql_query("SELECT * FROM tbl WHERE name='$name' AND week='$week'")) or die(mysql_error()); if($one == 0){ //make new row }else{ //edit current row } ?> If you don't know how to make rows or update them I will help with that too Quote Link to comment https://forums.phpfreaks.com/topic/62754-check-before-insert/#findComment-312359 Share on other sites More sharing options...
2tonejoe Posted July 31, 2007 Author Share Posted July 31, 2007 the update part. I struggle HARD with that one! How do I write a proper update? Quote Link to comment https://forums.phpfreaks.com/topic/62754-check-before-insert/#findComment-312362 Share on other sites More sharing options...
almightyegg Posted July 31, 2007 Share Posted July 31, 2007 $update = mysql_query("UPDATE tbl SET column1 = '$column1' AND column2 = '$column2'") or die(mysql_error()); They can be nasty, sometimes I get them wrong still... If you want to check it updated ok you can add: if(!$update){ echo "Error!?!?!?"; }else{ echo "Worked perfectly! "; } Quote Link to comment https://forums.phpfreaks.com/topic/62754-check-before-insert/#findComment-312366 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.