2

我编写了代码来尝试我的论坛上的用户活动,当我添加这一行时

$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 时,一切都恢复正常,但是我无法将当前活动用于配置文件。有什么建议么?

4

2 回答 2

5

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

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扩展名),您可以摆脱在值周围使用单引号。

于 2013-03-16T03:28:06.087 回答
3

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

于 2013-03-16T03:22:28.097 回答