简体   繁体   English

PHP SQL插入错误

[英]PHP SQL Insert Error

I have an error when inserting into the database. 插入数据库时​​出错。

Code: 码:

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

Ignore the dbquery, works exactly as mysql_query. 忽略dbquery,与mysql_query完全一样。

The error I am receiving is: 我收到的错误是:

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

No idea why this error is being thrown! 不知道为什么会抛出这个错误!

EDIT 编辑
I read too quickly the first time around; 我第一次看得太快; The error does not appear to be in the column list, it looks like it's in the value list. 该错误似乎不在列列表中,它看起来像在值列表中。 The only place the query can have a syntax error is if $article is empty (or un-sanitized data, such as non-numeric). 查询唯一可能出现语法错误的地方是$article是否为空(或未清理的数据,如非数字)。 Try adding quotes around it in the query and/or verifying it has at least a default value: 尝试在查询中添加引号和/或验证它至少具有默认值:

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

Original Answer 原始答案

There is a list of reserved words used by MySQL that, if you use them for column names, you have to escape them with backticks. 有一个MySQL使用的reserved words列表,如果你将它们用于列名,你必须用反引号来转义它们。

Try updating all of them to fix: 尝试更新所有这些以修复:

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

Teaching a man how to fish. 教一个人如何钓鱼。

If a query fails, the first thing you should do is to echo the query you're about to send: 如果查询失败,您应该做的第一件事是回显您即将发送的查询:

$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;

It's usually pretty obvious what's wrong with the final query; 通常很明显最终查询有什么问题; pay particular attention to the dynamic stuff in your query and generally around the area where MySQL complains about. 特别注意查询中的动态内容,通常是MySQL抱怨的区域。

If that still looks okay, then you look for words that might need escaping, such as the reserved words . 如果仍然看起来没问题,那么你会找到可能需要转义的单词,例如保留字

Conclusion 结论

Having looked at the code mysql, I would have to conclude that the problem lies with $article and it causes problems in your query. 看了代码mysql后,我不得不得出结论问题出在$article ,它会导致查询出现问题。 You should probably escape it as well, just in case :) 你也应该逃避它,以防万一:)

Recommendation 建议

You should learn about PDO / mysqli and using prepared statements: 您应该了解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' => '',
));

Thanks for the help guys! 谢谢你的帮助! But I fixed the problem! 但我解决了这个问题!

It seems that the cause of the problem was of the "URL". 似乎问题的原因是“URL”。 The URL was 网址是

news/1/&page=2 新闻/ 1 /页= 2&

So when I inserted the $article, it came as '1/', this was because it thought that the ID was 1/ , not 1 because of the URL. 因此,当我插入$ article时,它变为'1 /',这是因为它认为ID是1 /,而不是1,因为URL。

So I've just changed it to 所以我刚刚把它改成了

news/1&page=2 新闻/ 1页= 2

Thanks! 谢谢!

//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']."','')");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM