Jump to content

Check before insert


2tonejoe

Recommended Posts

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

<?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

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.