【问题标题】:PHP SQL Insert ErrorPHP SQL 插入错误
【发布时间】:2012-10-04 03:18:06
【问题描述】:

插入数据库时​​出错。

代码:

dbquery("INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ($article,'".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')");

忽略 dbquery,与 mysql_query 完全相同。

我收到的错误是:

 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 ''title','short','

不知道为什么会抛出这个错误!

【问题讨论】:

  • 为什么不先将所有数据放入变量中。以后更容易插入。例如:$title = clean($cmetss['title'])
  • 你可以只打印查询并在执行之前查看它是如何构建的

标签: php sql database


【解决方案1】:

教人如何钓鱼。

如果查询失败,您应该做的第一件事是回显您将要发送的查询:

$sql = "INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ($article,'".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')";

echo $sql;

最终查询出了什么问题通常很明显;请特别注意查询中的动态内容,并且通常围绕 MySQL 抱怨的区域。

如果看起来还可以,那么您可以查找可能需要转义的单词,例如 reserved words

结论

查看 mysql 代码后,我不得不得出结论,问题出在 $article 上,它会导致您的查询出现问题。您可能也应该逃避它,以防万一:)

推荐

您应该了解 PDO / mysqli 和使用准备好的语句:

// PDO example
$stmt = $db->prepare('INSERT INTO site_news_comments (articleid, title, short, comment, timestamp, userid, main, type, topstory) VALUES (:article, :title, :short, :comment, CURRENT_TIMESTAMP, :user, :main, :type, :topstory)');
$stmt->execute(array(
    ':article' => $article,
    ':title' => $commentss['title'],
    ':short' => '',
    ':comment' => $_POST['comment'],
    ':user' => USER_ID,
    ':main' => 0,
    ':type' => $commentss['type'],
    ':topstory' => '',
));

【讨论】:

  • 文章不是空的,因为它是定义在URL中的,其他的东西跟文章显示有关。
  • @zuc0001 也阅读了其余的答案,回显查询并查看它
  • 好的,我查询了。结果没问题,除了 $_POST['comment'] 有一个错误(但那是因为我没有提交表单),但是 $article 有一个错误,而不是 articleid 是 '1',它有'1/'。有什么想法吗?
  • @zuc0001 将回显查询添加到您的问题中,以便我们都可以查看......但显然,1/ 会导致问题:)
  • 谢谢你,如果不回显 SQL 查询,我将无法找到问题所在!非常感谢!
【解决方案2】:

编辑
我第一次读得太快了;该错误似乎不在列列表中,它看起来像在值列表中。查询可能出现语法错误的唯一地方是 $article 是否为空(或未经清理的数据,例如非数字)。尝试在查询中添加引号和/或验证它至少具有默认值:

$article = (empty($article) || !is_numeric($article)) ? 0 : $article;
dbquery("... VALUES ('".$article."', '".clean($commentss['title'])."', '', '".mysql_real_escape_string($_POST['comment'])."', current_timestamp, '".USER_ID."', '0', '".$commentss['type']."', '')");

原答案

有一个 MySQL 使用的 reserved words 列表,如果您将它们用作列名,则必须用反引号将它们转义。

尝试更新所有这些以修复:

dbquery("INSERT INTO site_news_comments (`articleid`, `title`, `short`, `comment`, `timestamp`, `userid`, `main`, `type`, `topstory`) VALUES ...

【讨论】:

  • 谢谢。尽管我仍然遇到完全相同的错误。它被用于我网站的“新闻”系统。当您从文章的“首页”发表评论时,它会插入 ok。但是当你从另一个cmets页面插入时,比如第2页,它会抛出错误。我不知道为什么。
  • -1。虽然转义列名是个好建议,但这不是根本原因。
  • @Jack 是的,与保留字列表相比,我实际上浏览了他所有的列名,但没有一个匹配(timestamp 除外,但那是“好的”);我更新了我的答案,但实际上看起来你的答案比我更胜一筹=P
【解决方案3】:

感谢大家的帮助!但是我解决了这个问题!

问题的原因似乎是“URL”。网址是

news/1/&page=2

所以当我插入 $article 时,它​​是 '1/',这是因为它认为 ID 是 1/ ,而不是 1 因为 URL。

所以我把它改成了

news/1&page=2

谢谢!

【讨论】:

    【解决方案4】:
    //change this line :
    dbquery("INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ($article,'".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')");
    
    //to this : (surround $articleid with single quote)
    dbquery("INSERT INTO site_news_comments (articleid,title,short,comment,timestamp,userid,main,type,topstory) VALUES ('".$article."','".clean($commentss['title'])."','','".mysql_real_escape_string($_POST['comment'])."',current_timestamp,'".USER_ID."','0','".$commentss['type']."','')");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 2017-04-28
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多