2

been learning php for 3 weeks now and i find myself with a simple error that does not make sense:

I cant see what is wrong with this code.

Could someone please point me to why this is happening.

Its a simple insert and set sql query which is like this:

code:

$insertresults = "UPDATE usage SET message='".$message."',islive='".$islive."' WHERE id=1"; 
$insertresults_doit = mysql_query($insertresults) OR die(mysql_error());  

the error i am getting is this:

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 'usage SET message='hello',islive='0' WHERE id=1' at line 1

as you can see, the variables are correct and to what i can see the sql string is correct also.

Thanks

5 Answers 5

1

Your table name, usage, is actually a reserved word in MySQL. You'll have to quote it with backticks:

UPDATE `usage` SET ...
Sign up to request clarification or add additional context in comments.

2 Comments

this didnt change the error message :( as this is within a class, would this make a difference?
you was correct about using usage, i changed the table name and now it works. Thanks for that, i have been scratching my head for about 2 days now. :)
0
$insertresults = "UPDATE usage SET message=$message,islive=$islive WHERE id=1"; 
$insertresults_doit = mysql_query($insertresults) OR die(mysql_error());  

You don't need to wrap the variables the way you did, give this a try :) Taking a look at that error shows you the extra ' surrounding the column names - you don't want that.

5 Comments

Don't wrap the variables at all then. Should work. If not, please give the new error. Thanks.
if the variables are INT then fine, but what if they are strings??
"UPDATE usage SET message='{$message}',islive='{$islive}' WHERE id=1"
@Nick i still get the exact same error if i use braces and even if i do not use the ' on int's the message is a message and the islive is either a 0 or a 1.
@Robert, have you made sure $message and $islive has been properly escaped? use: addslashes() or mysql_real_escape_string()
0

Try removing the quotes from $islive

Comments

0

Check whether you have a single quote in the variable values. It is always better to escape it before using in the sql statement

$message=addslashes($message);
$islive=addslashes($islive);

$insertresults = "UPDATE usage SET message='".$message."',islive='".$islive."' WHERE id=1"; 
$insertresults_doit = mysql_query($insertresults) OR die(mysql_error()); 

Assuming both the columns are of varchar type

Comments

0

"UPDATE usage SET message='{$message}',islive='{$islive}' WHERE id=1" – Nick 21 mins ago

@Nick i still get the exact same error if i use braces and even if i do not use the ' on int's the message is a message and the islive is either a 0 or a 1. – Robert 6 mins ago

@Robert, have you made sure $message and $islive has been properly escaped? use: addslashes() or mysql_real_escape_string()

moved to answer (grew)

1 Comment

yes just above this section the trim() htmlentities() and mysql_real_escape_string() are on these variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.