简体   繁体   中英

MySQL function Create Table gives me a Syntax error

I'm creating a login form, I created the database and now I'm creating the table with this function:

function CreateTable()
{
    $qry = "CREATE TABLE $this->tablename (
            'id' INT NOT NULL AUTO_INCREMENT ,
            'name' VARCHAR( 128 ) NOT NULL ,
            'email' VARCHAR( 64 ) NOT NULL ,
            'phone_number' VARCHAR( 16 ) NOT NULL ,
            'username' VARCHAR( 16 ) NOT NULL ,
            'password' VARCHAR( 32 ) NOT NULL ,
            'confirmcode' VARCHAR(32) ,
            PRIMARY KEY ('id')
            )";

    if(!mysql_query($qry,$this->connection))
    {
        $this->HandleDBError("Error creating the table \nquery was\n $qry");
        return false;
    }
    return true;
}

The problem is that every time I try to login to test the form I get this error:

Error creating the table query was CREATE TABLE ( 'id' INT NOT NULL AUTO_INCREMENT , 'name' VARCHAR( 128 ) NOT NULL , 'email' VARCHAR( 64 ) NOT NULL , 'phone_number' VARCHAR( 16 ) NOT NULL , 'username' VARCHAR( 16 ) NOT NULL , 'password' VARCHAR( 32 ) NOT NULL , 'confirmcode' VARCHAR(32) , PRIMARY KEY ('id') ) mysqlerror: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' INT NOT NULL AUTO_INCREMENT , 'name' VARC' at line 1

So obviously there's a syntax error on that line but as I have no experience in PHP I just can't see where the error is!

Any help very appreciated!

You are using quotes instead of back ticks. And yea, there's no tabe name being passed into the function it seems. Your function should look more like this:

function CreateTable($tableName)
{
    $qry = "CREATE TABLE IF NOT EXISTS $tableName (
            `id` INT NOT NULL AUTO_INCREMENT ,
            `name` VARCHAR( 128 ) NOT NULL ,
            `email` VARCHAR( 64 ) NOT NULL ,
            `phone_number` VARCHAR( 16 ) NOT NULL ,
            `username` VARCHAR( 16 ) NOT NULL ,
            `password` VARCHAR( 32 ) NOT NULL ,
            `confirmcode` VARCHAR(32) ,
            PRIMARY KEY (`id`)
            );";

    if(!mysql_query($qry,$this->connection))
    {
        $this->HandleDBError("Error creating the table \nquery was\n $qry");
        return false;
    }
    return true;
}

Do something like:

function Insert($tableName)
{
$tableName = newtable;
$insert_query = "insert into $tableName( name, email, username, password, confirmcode ) values ("bob", "bobsemailadress@hotmail.com", 911, "bob2013", "secrete", "1");";


    if(!mysql_query($qry,$this->connection))
    {
        $this->HandleDBError("Error inserting into the table \nquery was\n $qry");
        return false;
    }
    return true;
}

I don't know which code you have used for the insert, so I made some assumptions. Furthermore, it is basically another question.

table name is not came here

CREATE TABLE ( 'id' INT NOT NULL AUTO_INCREMENT , 'name' VARCHAR( 128 ) NOT NULL , 'email' VARCHAR( 64 ) NOT NULL , 'phone_number' VARCHAR( 16 ) NOT NULL , 'username' VARCHAR( 16 ) NOT NULL , 'password' VARCHAR( 32 ) NOT NULL , 'confirmcode' VARCHAR(32) , PRIMARY KEY ('id') )

It should be some thing like

CREATE TABLE `table` ( 'id' INT NOT NULL AUTO_INCREMENT , 'name' VARCHAR( 128 ) NOT NULL , 'email' VARCHAR( 64 ) NOT NULL , 'phone_number' VARCHAR( 16 ) NOT NULL , 'username' VARCHAR( 16 ) NOT NULL , 'password' VARCHAR( 32 ) NOT NULL , 'confirmcode' VARCHAR(32) , PRIMARY KEY ('id') )

so check your $this->tablename

Try surrounding $this->tablename with brackets, ie

$qry = "CREATE TABLE {$this->tablename} ( ... )";

Furthermore, you cannot use singlequotes to quote field names (these usually quote strings in SQL). You should use backticks (`) instead.

Why don't you just use HeidiSql to create your database and then make a connection you your form ???? it's much easier. And when using heidi you don't get any sql errors unless you write wrong php code for inserting,updating and deleting data into the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM