Page 1 of 1

Error in Syntax

Posted: Fri Oct 21, 2005 4:39 am
by mhouldridge
Hi,

I have the following insert query...

Code: Select all

$query = mysql_query("INSERT INTO mail (date, to, from, subject, message) 
        VALUES('now()', '$to', '$from', '$subject', '$message')")
		or die (mysql_error());

I get the error.....

You have an error in your SQL syntax near 'to, from, subject, message) VALUES('now()', '13', '8', 'Hello', 'How a' at line 1


I do not know why this is. Any thoughts?

Posted: Fri Oct 21, 2005 4:58 am
by shiznatix
don't put quotes around now()

also: isn't date a mysql function? that might be screwing you up probably

Posted: Fri Oct 21, 2005 4:59 am
by mhouldridge
Ok,

Removed quotes, but still erorring with sql syntax?

Thanks for the help so far!

Posted: Fri Oct 21, 2005 5:01 am
by shiznatix
then its date being a reserved word. change your table name to somting like ddate

Posted: Fri Oct 21, 2005 5:09 am
by n00b Saibot
hint: use `date` ;)

Posted: Fri Oct 21, 2005 5:14 am
by mhouldridge
Changed that to message_date but still same thing.


I have also taken the date out completely, but still the same thing.

could it be something to do with my table setup?

Posted: Fri Oct 21, 2005 5:46 am
by Jenk
post the (new) error message please.

Posted: Fri Oct 21, 2005 5:55 am
by mhouldridge
You have an error in your SQL syntax near 'to, from, subject, message, message_date) VALUES('10', '8', 'asdfa', '' at line 1

Posted: Fri Oct 21, 2005 6:07 am
by Jenk
do you definately have a field called 'to' on that table?

Is to a reserved variable/function/method/tag?

try

Code: Select all

$query = mysql_query("INSERT INTO `mail` (`date`, `to`, `from`, `subject`, `message`)
        VALUES('now()', '$to', '$from', '$subject', '$message')")
        or die (mysql_error());
As was suggested by n00b Saibot earlier.

Posted: Fri Oct 21, 2005 6:12 am
by mhouldridge
That's sorted it,

Thank you, and thanks to everyone who helped.

Curious, does the value -- --- ` ` bypass reserved words?

Mark

Posted: Fri Oct 21, 2005 7:29 am
by Weirdan
mhouldridge wrote: Curious, does the value -- --- ` ` bypass reserved words?
MySQL manual wrote: An identifier may be quoted or unquoted. If an identifier is a reserved word or contains special characters, you must quote it whenever you refer to it. For a list of reserved words, see Section 9.6, “Treatment of Reserved Words in MySQL”. Special characters are those outside the set of alphanumeric characters from the current character set, ‘_’, and ‘$’.

The identifier quote character is the backtick (‘`’):