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? 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 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? 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! "; } Link to comment https://forums.phpfreaks.com/topic/62754-check-before-insert/#findComment-312366 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.