2

I'm trying to do a very simple UPDATE with PHP, like this:

$nlk = $lk + "1";
mysql_query("UPDATE posts SET like = '".$nlk."' WHERE id = '".$cid."'") or die(mysql_error());

$lk is a the value gotten from the field like, which is default 0. $cid is a value from an id field, which is on auto_increment.

I get this 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 'like = '1' WHERE id = '45'' at line 1

What is the issue here?

Deniz Zoeteman
  • 9,691
  • 26
  • 70
  • 97

8 Answers8

5

like is a reserved word. You need to surround it with back-ticks

mysql_query("UPDATE posts SET `like` = '".$nlk."' WHERE id = '".$cid."'") or die(mysql_error());
sreimer
  • 4,913
  • 2
  • 33
  • 43
3

like is a reserved keyword. See here for a list of reserved keywords in mysql. If you enclose your like-Column in backticks (`), the error should go away.

theomega
  • 31,591
  • 21
  • 89
  • 127
2

Use this (added ticks (`) around the column name):

mysql_query("UPDATE posts SET `like` = '".$nlk."' WHERE id = '".$cid."'") or die(mysql_error());

Better yet, don't use reserved words as table/column names.

Czechnology
  • 14,832
  • 10
  • 62
  • 88
1

like is a MySQL keyword. It's most likely this is the case. Either try escaping the field name

mysql_query("UPDATE posts SET `like` = '".$nlk."' WHERE id = '".$cid."'") or die(mysql_error());

Or, if you're still getting the same error, change the field name to something else

jackbot
  • 2,931
  • 3
  • 27
  • 35
0

Like is a keyword in SQL. This could cause your error. Change your column name, or, at least, add the table name in front of your "like".

Raveline
  • 2,660
  • 1
  • 24
  • 28
0

because LIKE is a keyword. use backticks around like.

Raffael
  • 19,547
  • 15
  • 82
  • 160
0

like is a mysql reserved word

you have to put this column name in back quotes

mysql_query("UPDATE posts SET `like` = '".$nlk."' WHERE id = '".$cid."'") or die(mysql_error());
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
-2

The hint is in your error message, near WHERE id = '45"

This query will probably run if replace the double quotation mark with a single.

-gz

My bad, I missed the leading single in front of the reserved word like, and was viewing with non-monospace font so the two singles at the end looked like a double. Dur.

gz612
  • 1
  • 1