2012-10-04 51 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']."','')");