2012-11-18 126 views
0

我正在處理這段代碼,我正在使用一個簡單的插入語句,我無法弄清楚爲什麼它不工作。如果有人能看到我做錯了,請告訴我。謝謝! 這是我收到的錯誤:簡單的插入語句的問題

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 'long,comments) 
VALUES (2 ,2012-11-18 21:25:30, 39.3436984, -76.5856958, hh)' at line 1 

這裏是代碼:

mysql_query ("INSERT INTO incidents (emergency_type,date_time,lat,long,comments) 
VALUES (2 ,$catchDate, $catchLat, $catchLong, $catchDescription)") or die(mysql_error()); 
echo"<br /> Data inserted"; 
+0

就是這樣,感謝勞倫斯! – codenamejupiterx

回答

2

朗是一個保留字,試圖`long`與反引號代替包圍。

參考https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

圍繞文檔快速瀏覽發現,你應該調查PDO::preparePDO::execute做到這一點。您目前的方法似乎很容易受到SQL注入的影響。

我不是一個PHP程序員,但這樣的:

$db = get a db handle from somewhere 
$st = $db->prepare('Insert Into Incidents (emergency_type, date_time, lat, `long`, comments) Values (?, ?, ?, ?, ?)'); 
$st->execute(array(2 ,$catchDate, $catchLat, $catchLong, $catchDescription)); 
+0

更好的是,如果這是您自己的表格,請將這些列名稱更改爲「經度」和「緯度」。 – BellevueBob

0
INSERT INTO incidents (emergency_type,date_time,lat,`long`,comments) 
VALUES (2 ,$catchDate, $catchLat, $catchLong, '$catchDescription') 

LONGMySQL Reserved Keywords名單上。改爲用反向逃生。

還有一件事,date_timecomments的值必須用單引號括起來,因爲它們不是數字。

和您查詢是現在SQL Injection脆弱的,請花時間t閱讀文章下面

+0

哦,是插入字符串而不是綁定參數?如果是這種情況,海報應該尋找不同的方式。 – Laurence

+0

@Laurence是的,但目前,提問者並沒有參數化查詢。 –