PHP create table error 1064

前端 未结 5 2012
北恋
北恋 2020-12-22 06:18

I am trying to create a table in mySQL. This is my php page below, when I run the page there are no errors but the table is not in mySQL and when I test the code in mySQL I\

相关标签:
5条回答
  • 2020-12-22 06:30

    You need to escape int1 and int2. They are reserved words in MySQL

    CREATE TABLE Bookings 
    (
        id int(6) NOT NULL auto_increment, 
        name varchar(25),
        email varchar(35),
        number varchar(20),
        buffet varchar(3),
        ceilidh varchar(5),
        work1 varchar(3),
        beg1 varchar(3),
        `int1` varchar(3),
        adv1 varchar(3),
        youth varchar(3),
        lunch varchar(3),
        beg2 varchar(3),
        `int2` varchar(3),
        adv2 varchar(3),
        dinner varchar(3),
        dance varchar(5),
        work2 varchar(3),
        lunch2 varchar(3),
        price varchar(5),
        PRIMARY KEY  (ID)
    )
    
    0 讨论(0)
  • 2020-12-22 06:33

    You are using reserved words for column names. Check the following link to the mysql reference:

    http://dev.mysql.com/doc/refman/5.7/en/reserved-words.html

    0 讨论(0)
  • 2020-12-22 06:38

    Do not use any reserved words for column names. You need to change the column name int1 and int2 as they are reserved words. For more reserved words please see the link provided by @Alex Vogt.

    0 讨论(0)
  • 2020-12-22 06:38

    You need to understand the difference between PHP and SQL.

    You cannot put everything between <?php and ?> into mysql. This is the PHP part. In PHP you will connect to a MYSQL server and send a query to that server. I think you did take everything, because your error specifies near '$user = "root"' at line 1

    If you want to test your query, only take the part between $query = " and "; and paste that in mysql to test the query.

    So that would only be this part:

    CREATE TABLE Bookings 
    (
    id int(6) NOT NULL auto_increment, 
    name varchar(25),
    email varchar(35),
    number varchar(20),
    buffet varchar(3),
    ceilidh varchar(5),
    work1 varchar(3),
    beg1 varchar(3),
    int1 varchar(3),
    adv1 varchar(3),
    youth varchar(3),
    lunch varchar(3),
    beg2 varchar(3),
    int2 varchar(3),
    adv2 varchar(3),
    dinner varchar(3),
    dance varchar(5),
    work2 varchar(3),
    lunch2 varchar(3),
    price varchar(5),
    PRIMARY KEY  (ID)
    )
    

    Once you do that, juergen d's answer might apply: escape int1 and int2 by changing them into `int1` and `int2` to escape reserved keywords.

    0 讨论(0)
  • 2020-12-22 06:39

    You have problem with your column name(s)

    int1s varchar(3),
    

    and

    int2 varchar(3),
    

    change that to something else or enclose them in backticks.

    Like this...

    CREATE TABLE Bookings 
    (
    id int(6) NOT NULL auto_increment, 
    name varchar(25),
    email varchar(35),
    number varchar(20),
    buffet varchar(3),
    ceilidh varchar(5),
    work1 varchar(3),
    beg1 varchar(3),
    `int1` varchar(3), /* see here... */
    adv1 varchar(3),
    youth varchar(3),
    lunch varchar(3),
    beg2 varchar(3),
    `int2` varchar(3), /* see here... */
    adv2 varchar(3),
    dinner varchar(3),
    dance varchar(5),
    work2 varchar(3),
    lunch2 varchar(3),
    price varchar(5),
    PRIMARY KEY  (ID)
    )
    
    0 讨论(0)
提交回复
热议问题