2012-10-04 52 views
1

插入数据库时​​出错。PHP SQL插入错误

代码:

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

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

+0

为什么不把所有的数据都放入变量中。稍后更容易INSERT。例如:$ title = clean($ commentss ['title']) – s3polz

+0

您可以打印查询并查看它是如何构建的,然后执行 – Deepak

回答

2

教人如何钓鱼。

如果查询失败,你应该做的第一件事就是来呼应你要发送的查询:

$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' => '', 
)); 
+0

文章不是空的,因为它是在URL中定义的,还有其他与文章展示有关的内容。 – zuc0001

+0

@ zuc0001阅读其余的答案,回应查询,看看它 –

+0

好吧,我做了一个查询。它出来没问题,除了$ _POST ['comment']的一个错误(但那是因为我没有提交表单),但是在$ article文件中出现错误,而不是articleid为'1',它有'1 /'。有任何想法吗? – zuc0001

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 ... 
+0

谢谢。虽然我仍然得到完全相同的错误。它被用于我的网站的“新闻”系统。当您从文章的“首页”发表评论时,它会插入正常。但是当你从另一页的评论中插入它时,如第2页,它会引发错误。我不知道为什么。 – zuc0001

+0

-1。尽管转义列名是个好建议,但它不是根本原因。 –

+0

@Jack是的,我实际上浏览了所有列名,而不是保留字列表,没有匹配(除了'timestamp',但这是一个“好的”)。我更新了我的答案,但实际上它看起来像你打我一拳= P – newfurniturey

0

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

看起来问题的原因是“URL”。网址已

新闻/ 1/& = 2

所以,当我插入$文章,它来为1 /页面,这是因为它认为ID为1 /,而不是1,因为的网址。

所以我只是把它改为

新闻/ 1 &页= 2个

谢谢!

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