2010-12-02 66 views
1

大家好我有一個字段「ammount」在mysql數據庫中有「varchar(50)」類型。當我插入數據到該領域例如ammount = 4公斤它確定但當我更新該字段時,它給了我以下錯誤。更新查詢問題

Error in query: UPDATE ingredients SET ingredient_name='test recipe',ammount=4 gm where ingredient_id='59'. 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 'gm where ingredient_id='59'' at line 1

和我的查詢是

 $query="UPDATE ingredients SET ingredient_name='$ingredient',ammount=$ammount where ingredient_id='$ingredient_id'";

回答

2

1)正確的拼寫爲 「量」。

2)對於SQL查詢,您不應該使用像這樣的變量插值。這是非常不安全的。使用prepared statement

3)定義$query時,您沒有在$amount附近引用引號,所以它們不會在最終的替換查詢字符串中結束。仔細查看錯誤消息:它顯示了SQL試圖處理的查詢。注意它如何說ammount=4 gm?它無法處理,因爲沒有引號。

如果您use prepared statements像你應該的那樣,引用照顧自己。

1

您的查詢有:

...,ammount=4 gm where... 

這是不正確。您需要在4 gm附近引用。

變化

,ammount=$ammount where 

,ammount='$ammount' where 
+0

它會「工作」,但請不要這樣做!如果`$ amount`來自用戶,他可以將其設置爲''; DROP TABLE成分; - ,他擦掉了你的桌子。 – 2010-12-02 07:02:18

+0

您如何確定'$ ammount`在查詢中被使用之前未經過清理? – codaddict 2010-12-02 07:04:46