【问题标题】:Warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL警告:您的 SQL 语法有错误;检查与您的 MySQL 对应的手册
【发布时间】:2013-07-25 18:17:35
【问题描述】:

警告:您的 SQL 语法有错误;查看与您的 MySQL 服务器版本相对应的手册,了解在 'color:red' 附近使用的正确语法>

这是我的代码:

$db->query("UPDATE members set id='{$this->test['id']}', 
     lvl='{$this->userlvl}', ip='{$this->test['IP']}', 
     time='{$this->test['time']}', 
     linechat='{$this->test['msg']}' 
     WHERE user='{$this->test['name']}'");

我是初学者,所以请告诉我必须是什么^^

我试过了

$fixchat = mysql_real_escape_string($this->test['msg']);
$fixname = mysql_real_escape_string($this->test['name']);

$db->query("UPDATE members set id='{$this->test['id']}',
     lvl='{$this->userlvl}', ip='{$this->test['IP']}', 
     time='{$this->test['time']}', linechat='{$fixchat}' 
     WHERE user='{$fixname}'");

但我收到了这个错误:

警告:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''\wellington\'' 附近使用正确的语法...

【问题讨论】:

  • 替换后打印出查询。
  • 我得到了这个:'set id='11276760', lvl='member', ip='', time='1374777237', linechat='{ .. 它没有完成,因为语法错误
  • 。 .将 sql 存储在单独的变量中,打印出来,然后运行。

标签: mysql sql syntax


【解决方案1】:

$this->test 数组中的一个或多个值包含双引号字符。查询字符串的每个动态元素都必须通过适当的转义函数(在您的情况下为mysql_real_escape_string())进行转义。这将转义引号,以便正确解释字符串。

旁注:您应该使用 mysqli PHP 库而不是已弃用的 mysql。此外,更好的替代解决方案是使用参数化查询。

【讨论】:

  • 嗨,我能知道它应该是什么吗?
  • 应该是什么?代码?编写一组新的变量集,通过转义函数传递现有变量。
【解决方案2】:

解决方法:addlashes 函数

$fixchat = addslashes($this->test['msg']);
$fixname = addslashes($this->test['name']);

$db->query("UPDATE members set id='{$this->test['id']}',
     lvl='{$this->userlvl}', ip='{$this->test['IP']}', 
     time='{$this->test['time']}', linechat='{$fixchat}' 
     WHERE user='{$fixname}'");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多