View Full Version : why is this query failing?

09-14-2006, 06:12 AM

Can someone please tell me why the following query is failing?

$qryupdate = "INSERT INTO Warranty (servicetag, description, provider, startdate, enddate, daysleft) " .
"VALUES ('$servicetag', '$description', '$provider', '$startdate', '$enddate', '$daysleft') " .
"WHERE (servicetag='$servicetag', description='$description', provider='$provider', " .
"startdate='$startdate', enddate='$enddate')";

$results = mysql_query($qryupdate);



09-14-2006, 06:15 AM
You can't have a where clause on an insert statement. Use update if that's what you need to do.

Good luck;

09-14-2006, 06:31 PM
I want to replace the old values with the new values. UPDATE doesn't work because if the old values are the same as the new values it fails, which is fine, but the way the rest of my code is designed:

if (mysql_affected_rows() == -1)

$qryinsert = "INSERT INTO Warranty (servicetag, description, provider, startdate, enddate, daysleft) " .
"VALUES ('$servicetag', '$description', '$provider', '$startdate', '$enddate', '$daysleft') ";

$results = mysql_query($qryinsert);


...so the information is just inserted into the table instead of being overwritten/updated. Make sense?
INSERTing is fine if the information doesn't already exist in the table. But if the information already does exist in the table, I want to update it because, more then likely, the information in $daysleft will change daily.



09-14-2006, 07:28 PM
See the manual for insert on duplicate update (http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) that is what you should use.