...

View Full Version : INSERT mysql query



Jon W
03-03-2009, 04:00 AM
Whats wrong with this query?


mysql_query("INSERT INTO users (`last_ip`) VALUES ('$new_ip') WHERE user_id='".$active['user_id']."' ") or die('Server Error: ' . mysql_error());



I get this syntax error:

Server Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE user_id='1'' at line 1

Old Pedant
03-03-2009, 04:03 AM
You can't use a WHERE clause with an INSERT. Period.

Doesn't make sense: You are creating a *NEW* record. Completely new. From scratch.

A WHERE clause is always used to select some *existing* record or records.

Jon W
03-03-2009, 04:09 AM
Okay, so I would have to use the update, right?

Old Pedant
03-03-2009, 08:20 AM
Dunno. Only you can tell us.

If the record does not exist, you can't update it. Then you must insert it. But yes, if it exists you must update.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum