hunter
hunter

Reputation: 1101

update query problem

hi all i have a field "ammount" in mysql database which have "varchar(50)" type. When i insert data into that field e.g ammount= 4 kg its ok but when i update that field it gives me the following error.

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

and my query is

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

Upvotes: 1

Views: 125

Answers (2)

Karl Knechtel
Karl Knechtel

Reputation: 61508

1) The correct spelling is "amount".

2) You should not be using variable interpolation like this for an SQL query. It is very unsafe. Use a prepared statement.

3) You didn't put quotes around $amount when defining $query, so they don't end up in the final substituted query string. Look closely at the error message: it shows you the query that SQL tried to process. Notice how it says ammount=4 gm? It can't handle that, because there are no quotes.

If you use prepared statements like you are supposed to, the quoting takes care of itself.

Upvotes: 2

codaddict
codaddict

Reputation: 454970

Your query has:

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

which is incorrect. You need quotes around 4 gm.

Change

,ammount=$ammount where

to

,ammount='$ammount' where

Upvotes: 1

Related Questions