alsweet alsweet - 28 days ago 6
SQL Question

unusual sql error

so i've been stuck on this sql error for some time.

this is the SQL line i'm using:

INSERT INTO images (image_name, orientation, restored, commercial, automotive, bespoke, before, after, date_added)
VALUES ('image4-after.jpg', 'portrait', '1', '1', '1', '1', '24', '5', '2012-07-08')


using this structure:

image_id - int(11) AUTO_INCREMENT
image_name - varchar(40)
orientation - varchar(4)
restored - tinyint(1)
commercial - tinyint(1)
automotive - tinyint(1)
bespoke - tinyint(1)
before - int(11)
after - int(11)
date_added - date


getting the error message:


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 'before, after, date_added) VALUES ('image4-after.jpg',
'portrait', '1', '1', '1'' at line 1


can anybody please tell me what i've done wrong?

thanks

Answer

BEFORE is a MySQL reserved keyword. You need to quote it with backticks to use it as a table or column identifier.

INSERT INTO images (image_name, orientation, restored, commercial, automotive, bespoke, `before`, after, date_added) 
VALUES ('image4-after.jpg', 'portrait', '1', '1', '1', '1', '24', '5', '2012-07-08')

AFTER is not reserved, however.

Whenever a 1064 error points to something non-obvious syntactically in its right syntax to use near... indicator, look to the reserved words list.

Comments