您的SQL语法有错误;查看与MySQL服务器对应的手册

时间:2013-03-16 03:14:23

标签: php mysql sql

我制作了代码,在我的论坛上尝试用户活动,当我添加此行时

$cat_id = $db->fetch("SELECT name FROM " . $prefix . "_categories WHERE id =" . mysql_real_escape_string($forum_data['cat_id']));
$page_title_pro = ' > ' . $system->present($cat_id['name']) . ' > ' . $system->present($forum_data['name']) . '';

我得到了

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 's Treasures > Contests' WHERE id = '2'' at line 1

我假设2是我的用户ID,而在页脚中,我有这个:

$db->query("UPDATE accounts SET flocation = '$session_location', page = '$page_title_pro'  WHERE id = '$id';");

我似乎无法找到错误,并且当我将cat_id取出时,每个都恢复正常,但之后我无法使用当前活动进行配置文件。有什么建议?

2 个答案:

答案 0 :(得分:4)

您的更新语法没有问题。问题在于您要在包含单引号的特定列上设置的值,这会导致您的更新语法中断。在将值传递给查询之前,您需要转义值中的单引号。一种可能的方法是使用

<强> mysql_real_escape_string

$val1 = mysql_real_escape_string($session_location);
$val2 = mysql_real_escape_string($page_title_pro);
$val3 = mysql_real_escape_string($id);
$db->query("UPDATE accounts SET flocation = '$val1', page = '$val2'  WHERE id = '$val3'");

另一个(首选)是使用PreparedStatementsPDOMySQLi扩展程序),您可以摆脱使用值周围的单引号。< / p>

答案 1 :(得分:3)

mysql_real_escape_string您的更新查询的输入值,来自上一个查询的$page_title_pro,其中包含'