开发者

MySQL issue with UPDATE numbers [duplicate]

This question already has an answer here: Syntax error due to using a reserved word as a table or column name in MySQL (1 answer) Closed 8 years ago.

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?


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());


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.


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.


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


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".


because LIKE is a keyword. use backticks around like.


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());


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜