2010-01-23 132 views
1

我絕不經歷了MySQL和不斷收到錯誤代碼這樣的行:神祕MySQL錯誤

$sql= "INSERT INTO songs (unique_show_id, artist, date, year, city, state, venue, taper, transfered_by, source, mic_loc, lineage, uploaded_by, uploaded_on, show_notes, show_xml) 
      VALUES('$showId', '$artist', '$showDate', '$year, '$city', '$state', '$venue', '$taper', '$transferer', '$source', '$mic_loc', '$lineage', '$uploader', NOW(), '$show_notes', '$show_xml')";  

//check to see if the query went through 
      if (!mysql_query($sql,$con)){ 
       echo "query fail"; 
      die('Error: ' . mysql_error()); 
    } 

我敢肯定它的東西簡單,但我不能看到那裏的錯誤是。該錯誤消息我得到的是:

query failError: 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 'ipuhgbi', 'CA', '', '', '', '', '', '', 'danwoods', NOW(), '', '<show id=\'gm198' at line 2 

一些我插入值是NULL,但是從我讀過,我不認爲這應該是一個問題。有任何想法嗎?

+0

顯示替換的查詢。 – 2010-01-23 06:20:18

+0

請在MySQL中發佈'DESCRIBE SONGS'的輸出,以便我們可以看到每列的數據類型。 – 2010-01-23 06:25:39

回答

0

它看起來像錯誤行上的最後一個單引號不會被轉義。

您需要記住清理所有進入查詢的字符串。

0

有你需要把握相當幾件事:

  1. 你不通過查詢插入主鍵(例如unique_show_id在你的代碼)
  2. 對於數字你不使用單引號。
  3. 這是更好地使用插入記錄其避免瞭如計數問題集變種:對數字
  4. 使用INTVAL和mysql_real_escaps_string字符串,以避免注射問題以及單引號查詢錯誤回報。

    插入到表組字段= 'FIELD_VALUE',FIELD2 = 'FIELD_VALUE' //等

4

$year後缺少引號。

當MySQL發出這樣的錯誤(接近bla di bla)時,錯誤通常在它提到的字符串之前。在這種情況下,'ipuhgbi'映射到$ city,所以你知道它在'$ city'之前,我們在那看到什麼?瞧,一個缺少的報價。