更新时出现SQL错误

时间:2013-09-26 02:05:58

标签: php mysql

运行PHP SQL代码时,我遇到SQL错误:

  

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

mysql_query("UPDATE free_ebook SET math = $assign_math WHERE useid = $newuserid;")or die(mysql_error());

math字段为int(10)useid也为int(10)

1 个答案:

答案 0 :(得分:1)

看起来字段名称应该是userid,而不是useid

或者,尝试将值括在单引号中,如下所示:

"UPDATE free_ebook SET math = '".$assign_math."' WHERE useid = '".$newuserid."';"

甚至

"UPDATE free_ebook SET math = '{$assign_math}' WHERE useid = '{$newuserid}';"

关于主题:mysql_query()已弃用,您应该使用PDO扩展程序。这很容易(也许更容易!)学习和更安全:

Are there good tutorials on how to use PDO?

http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html