Error in Syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Error in Syntax

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

don't put quotes around now()

also: isn't date a mysql function? that might be screwing you up probably
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Ok,

Removed quotes, but still erorring with sql syntax?

Thanks for the help so far!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

then its date being a reserved word. change your table name to somting like ddate
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

hint: use `date` ;)
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

post the (new) error message please.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

You have an error in your SQL syntax near 'to, from, subject, message, message_date) VALUES('10', '8', 'asdfa', '' at line 1
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

That's sorted it,

Thank you, and thanks to everyone who helped.

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

Mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 (‘`’):
Post Reply