views:

1092

answers:

5

No doubt I'm missing something really simple here but I just can't see the problem with this query which is producing the following error:

SQL query:

INSERT INTO ads(
    ad_id, author, ad_date, category, title,
    description, condition, price, fullname,
    telephone, email, status, photo, photothumb
)
VALUES (
    NULL , 'justal', '1225790938', 'Windsurf Boards',
    'test', 'test', 'Excellent', '12', 'test',
    'test', 'test', '', '', ''
);

MySQL said: Documentation
#1064 - 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 ''ad_id', 'author',
'ad_date', 'category', 'title', 'description',
'condition', '' at line 1

Can someone with a fresh pair of eyes spot the problem?

Thanks, Al.

+3  A: 

Shouldn't you use back ticks instead of single quotes in column names?

INSERT INTO ads( `ad_id`, `author`, `ad_date`, `category`, `title`, `description`, `condition`, `price`, `fullname`, `telephone`, `email`, `status`, `photo`, `photothumb` )
VALUES (
NULL , 'justal', '1225790938', 'Windsurf Boards', 'test', 'test', 'Excellent', '12', 'test', 'test', 'test', '', '', ''
);
Alexander Kojevnikov
Yes, but not using quotes at all is eassier to read (and if your column names are such that you need to quote them, you chose the wrong column names)
Mark Baker
i completely disagree. using backticks at all times is great for readability and saves you having to make up arbitrary names for fields when the perfect name would be "key", "value", etc.
nickf
I tend to agree with Nick.
Alexander Kojevnikov
and *I* agree with Alexander! :-p
nickf
I agree with you two. :p
andyk
backticks seems to have fixed the issue so maybe one of the column names (possibly status or condition) did need them? ;)
Alan
It's `condition`, see the answer from @gusmos
Alexander Kojevnikov
+1  A: 

Single quotes are used for string literals. In MySQL, by default, double quotes are also used for string literals although this is incompatible with standard SQL and you should stick to single quotes in your code.

For column names, you normally wouldn't quote them at all. If you need to - and you don't for any of yours - then quote them with a backquote (`), or set it to strict ANSI compatible mode (ANSI_QUOTES) and use double quotes.

Mark Baker
A: 

Thanks.... I didn't originally have any quotes on the column names and that didn't work either.

SQL query:

INSERT INTO ads( ad_id, author, ad_date, category, title, description, condition, price, fullname, telephone, email, status, photo, photothumb )
VALUES (
'', 'justal', '1225790938', 'Windsurf Boards', 'test', 'test', 'Excellent', '12', 'test', 'test', 'test', '', '', ''
)

MySQL said: Documentation
#1064 - 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 'condition, price, fullname, telephone, email, status, photo, photothumb) VALUES ' at line 1

This query was working fine until the server was updated to php5 if that helps anyone?

Thanks, Alan.

Alan
Does my version work? On a side note, you should rather edit your original question or add a comment to the corresponding answer than adding a new answer.
Alexander Kojevnikov
Yes, thanks Alexander, your version (with the backticks) does work and thanks for the pointer regarding comments rather than answers too.
Alan
A: 

Aha.... The backticks seems to do the trick... I think the column with the name status was causing the problems.

Thanks, Al.

Alan
+1  A: 

It might be that CONDITION is a mysql keyword, and not allowed as column name.

http://dev.mysql.com/doc/refman/5.1/en/declare-conditions.html

You definitely do not need the ' or `

if you had a column name of "condition" and it was a reserved keyword, wouldn't that mean you definitely DO need the backtick?
nickf