tags:

views:

40

answers:

5

Hello, can't understand mysql error:

UPDATE static_pages SET order = " Some new data 222222

"Database 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 'order = "

$query = 'UPDATE someTable SET '.$key.' = "'.$value.'"';

Here is $key = order; $value = 'new data 222222'; There is such keys in table: order, prices, contacts. Each of these updates well except the 'order'. Why?

+8  A: 

ORDER is a reserved word.

Use back ticks to escape the column name:

UPDATE static_pages SET `order` = ";

Don't forget the WHERE clause so that you can update only specific records.

UPDATE static_pages SET `order` = "
WHERE id = 12;
Marcus Adams
Yeah, I've totally forgot about reserved words. Thank you.
Ockonal
A: 

Where is WHERE

Have to use WHERE clause with update buddy

nik
You do not *have* to use a `WHERE` clause in an `UPDATE` statement. It is just very useful if you do not want to update every row in the table :)
Jørn Schou-Rode
+1  A: 

order is a keyword in SQL. protect it with quotes in your query.

Best option is to rename your 'order' field

If you can't, here's a possible solution:

$query = 'UPDATE someTable SET `'.$key.'` = "'.$value.'"';
Rodolphe
A: 

use `around the key`

$query = "UPDATE `someTable` SET `$key` = '$value'";
Moak
A: 

You can encase the reserved word orders into backticks '`' or rename the field.

Raveren