1

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?

Max Frai
  • 61,946
  • 78
  • 197
  • 306

5 Answers5

8

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
  • 53,009
  • 9
  • 91
  • 143
1

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
  • 507
  • 2
  • 7
0

Where is WHERE

Have to use WHERE clause with update buddy

nik
  • 3,688
  • 3
  • 21
  • 33
  • 1
    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 Apr 14 '10 at 15:36
0

use `around the key`

$query = "UPDATE `someTable` SET `$key` = '$value'";
Moak
  • 12,596
  • 27
  • 111
  • 166
0

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

raveren
  • 17,799
  • 12
  • 70
  • 83