Hi,
I have a table, and I need to be able to modify records, and if the record doesn't exist add a new record. What is the easiest way to do this? Can to do..
$sql="SELECT * FROM table WHERE value2='$value2'";
$result=db_query($sql);
if (!$result || db_numrows($result) > 1) {
$sql="UPDATE table SET value1='$value1' WHERE value2='$value2'";
$result=db_query($sql);
return true;
} else {
$sql="INSERT INTO table(value1,value2) ".
"VALUES ('$value1','value2')"; $result=db_query($sql);
return true;
}
This way you enter a value, if it exists it is updated, if not it is added.
I have also been thinking about having a checkbox, so if its checked the record is updated, if not you get an error returned, asking if you want to update, and if so check the box.
Cheers
/voodoo
I have a table, and I need to be able to modify records, and if the record doesn't exist add a new record. What is the easiest way to do this? Can to do..
$sql="SELECT * FROM table WHERE value2='$value2'";
$result=db_query($sql);
if (!$result || db_numrows($result) > 1) {
$sql="UPDATE table SET value1='$value1' WHERE value2='$value2'";
$result=db_query($sql);
return true;
} else {
$sql="INSERT INTO table(value1,value2) ".
"VALUES ('$value1','value2')"; $result=db_query($sql);
return true;
}
This way you enter a value, if it exists it is updated, if not it is added.
I have also been thinking about having a checkbox, so if its checked the record is updated, if not you get an error returned, asking if you want to update, and if so check the box.
Cheers
/voodoo