Question:
PHP-MYSQL UPDATE SYNTAX ERROR?
Wes D
2009-02-17 21:04:59 UTC
I have the following query to update a database table from a form and when I run it, I get a syntax error.

$query = "UPDATE dues SET jan='$jan', feb='$feb', mar='$mar', apr='$apr', may='$may', jun='$jun', jul='$jul', aug='$aug', sep='$sep', oct='$oct', nov='$nov', dec='$dec' WHERE ID=$id";
$result = mysql_query($query);

when I echo mysql_error(); I get the following:
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 'dec='Not Paid' WHERE 'ID'=1' at line 1

I have already tried putting single quotes around $id but that didn't fix it either, I tried removing the commas and it didn't work either. I tried putting the values in parenthases and it didnt work. I have tried a lot of things and NOTHING has worked. I have no idea what I need to change.

I need help because I usually set up all my other update statements just like this one, (syntax-wise), but for some reason this one wont work.

If you need more details to figure it out, just let me know.

Thank You!!
Four answers:
plavacek
2009-02-17 21:39:27 UTC
DEC is a reserved word in MySQL. See: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html



Try ....

nov='$nov', 'dec'='$dec' WHERE ID=$id;



or, possibly better,

nov='$nov', dues.dec='$dec'



If neither of those help, please print out the "real" SQL you execute and update the problem with it - it might be something that is only causing syntax problems once the variables are resolved.
George Edison
2009-02-17 21:34:30 UTC
It looks like the value of $nov has an invalid character in it. Check what it contains. Check the other values as well. What I did is create a function called MakeMySQLSafe that escapes the strings going into MySQL. It would be a great idea anyway to prevent MySQL injection attacks, which can be VERY serious.
?
2016-10-03 01:37:22 UTC
Does it Specify which line? and devoid of greater info bearing directly to the blunders its complicated to work out what must be going incorrect. there's a omit-spelling of highway on your replace question and replace is spelt incorrect as nicely i dont comprehend if theese are basically blunders on your question and not on your code. greater info could be liked, thank you.
2009-02-17 22:36:18 UTC
Hm....



















Your query value is

UPDATE dues

SET bla...bla...bla, dec='???'

WHERE ID=?";



DEC is keyword mysql. Fix it!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...