tags:

views:

131

answers:

6
INSERT INTO `test` (`x`, `y`)  WHERE `id` = `$id`

VALUES (`$x`, `$y`)

Whats wrong with this query? I run this in a mysql_query() function in a php file.

+6  A: 

You can't use a Where clause with an insert. You are either inserting a row or you're not.

Thomas Jones-Low
+4  A: 

If you're trying to update information from the database, use UPDATE instead of INSERT INTO in the query you're running.

James
A: 

You need to remove the "WHERE id=$id" - it has no meaning in an INSERT statement.

Jarod Elliott
+2  A: 

You can't use a where clause on an insert. I think you might be wanting an update statement:

update test set
x = $x,
y = $y
where id = $id

When you're inserting a new value in the database, you usually don't have an ID value until after the insert (assuming you're using auto-generated IDs).

Andy White
A: 

So, either

UPDATE test SET x='x', y='$y' WHERE id ='$id';

OR

INSERT INTO test ('x', 'y') VALUES ('$x', '$y');

as stated in other posts - you cannot do an INSERT with a WHERE.

Mark Unwin
A: 

Also note that you must use single quotes (') rather than backticks (`) for values. Backticks are used when referencing column names / field names.

This:

`field` = '$value'

rather than this:

`field` = `$value`

unless you really do want to reference another field. This is sometimes what you want when copying values or matching JOINs and such. But because you have a variable there, I'm assuming you want to use single quotes rather than backticks.

jedihawk