Search code examples
mysqlsqlsyntax-errormysql-error-1064

Can MySQL INSERT Query be run out of order


I am attempting to run:

INSERT INTO orders ('id','datetime','server','price','comment','addons','table_number','Omelet','Pancakes','Crepes','Salad','Steak_Sandwich','Chicken_Quesadilla','BLT','Prime_Rib','Pineapple_Upside_Down_Cake') VALUES ('','18/9/11 6:49:03PM','admin','0', 'N/A','None','0','1','2','3','4','5','6','7','8','9')

and phpmyadmin columheaders look like:

    id  datetime    server  price   comment addons  table_number    Omelet  Steak_Sandwich  Chicken_Quesadilla  BLT Salad   Prime_Rib   Crepes  Pancakes    Pineapple_Upside_Down_Cake

and is returning:

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 ''id','datetime','server','price','comment','addons','table_number','Omelet','Pan' at line 1

Can I do this out of order like I have it and if so can anyone spot my error Thanks in advance


Solution

  • As long as id is a column name - you need to remove single quotes and replace them with backticks

    INSERT INTO orders (`id`, `datetime`, ...
    

    Also you can remove backticks too and specify id as-is, but this can cause an issue if your column name has the same name as one of mysql's keywords, such as from

    UPD: As someone pointed in the comments (sorry, I don't remember your name and the comment has been already deleted) - if id is an autoincrement field, then you can avoid specifying it at all (if so - don't forget to remove its value (empty string) from values list too)