duanke8011 2013-03-16 03:14
浏览 757
已采纳

您的SQL语法有错误; 检查与MySQL服务器对应的手册[关闭]

I made code to try a users activity on my forum, and when I add in this line

$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']) . '';

I get

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

I am assuming that the 2 is my user id, and in the footer, I have this :

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

I can't seem to find the error, and every goes back to normal when i take the cat_id out, but then i can't use the current activity for the profiles. Any suggestions?

  • 写回答

2条回答 默认 最新

  • dtjw6660 2013-03-16 03:28
    关注

    There is no problem with your update syntax. The problem is with the values you want to set on specific column that contains single quote which causes to break your update syntax. You need to escape the single quotes in the value before passing it on the query. One possible way is by using

    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'");
    

    Another (the PREFERRED one) is by using PreparedStatements (PDO or MySQLi extensions) you can get rid of using single quotes around values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划