0

I keep getting error in my code : "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 '$sql = "CREATE TABLE Kodu( ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, ' at line 1"

I even tried this code in SQLfiddle and it gave me the same error. Can somebody please help me spot the error? thanks

  $sql = "CREATE TABLE Kodu(
    ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    Dateoforder DATE NOT NULL,
    Contract VARCHAR(10),
    Order INT NOT NULL,
    Office VARCHAR(30) NOT NULL,
    Ship VARCHAR(100) NOT NULL 

)";
kiq
  • 39
  • 4

2 Answers2

1

Try this:

CREATE TABLE Kodu(
    ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    Dateoforder DATE NOT NULL,
    Contract VARCHAR(10),
    OrderNumber INT NOT NULL,
    Office VARCHAR(30) NOT NULL,
    Ship VARCHAR(100) NOT NULL 
)

You need to change Order with another name because order is a protected word in SQL

Or wrap into quotes or backtick the word like this (if ANSI mode is enabled):

   CREATE TABLE Kodu(
        ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
        Dateoforder DATE NOT NULL,
        Contract VARCHAR(10),
        "Order" INT NOT NULL,
        Office VARCHAR(30) NOT NULL,
        Ship VARCHAR(100) NOT NULL 
    ) 
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
1

The issue is the keyword order.

You have to escape the work either using back ticks or surrounding with quotes if ansi mode is enabled.

Please read more How do I escape reserved words used as column names? MySQL/Create Table

The better recommendation is to use another word instead of escaping.

Avitus
  • 15,640
  • 6
  • 43
  • 53