0

我有这部分代码:

$result = mysql_query("SELECT * FROM posts WHERE id = '$id'") or die (mysql_error());
$rec = mysql_fetch_array($result) or die (mysql_error());
$like = $rec['like'];
$like += 1;
mysql_query("UPDATE posts SET like = '$like' WHERE id = '$id'") or die (mysql_error());

并返回此错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“like = '1' WHERE id = '43'' 附近使用正确的语法”

4

4 回答 4

4

LIKE是一个保留的 MySQL 关键字。引用您的列名以避免将它们误认为是关键字:

... SET `like` = ...
于 2012-07-02T15:16:22.707 回答
1

您正在使用保留关键字LIKE

试试这样:

"UPDATE `posts` SET `like` = '$like' WHERE `id` = '$id'"
于 2012-07-02T15:16:04.243 回答
0

Your problem is that like is a mysql keyword, either change the column name in your database or surround with backticks.

Also dont use mysql_ functions use mysqli or PDO as what you have is not safe and outmoded

于 2012-07-02T15:18:01.067 回答
0

"like" is a reserved word, it is not recommended to use it to name columns. If it is not possible for you to rename the column, modify your UPDATE statement by escaping it with backticks :

`like`
于 2012-07-02T15:20:53.787 回答