2014-02-26 106 views
0

這是我的代碼。但是每次創建表tblStudents時顯示錯誤。這是爲什麼?儘快需要解決方案。PHP MySQL無法創建表格

<?php 

$con=mysqli_connect("localhost","root","noor.xbyte","fathis_quran_class"); 
if (mysqli_connect_errno()) { 
    echo '&lt;h1>Error Connecting to the database!&lt;/h1>'; 
} else { 
    $sql = "CREATE TABLE tblStudents 
    (
    index INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(index), 
    fullName CHAR(30) NOT NULL, 
    dateOfBirth DATE NOT NULL, 
    SID TINYINT NOT NULL, 
    address CHAR(30) NOT NULL, 
    level TINYINT NOT NULL,)"; 
    if (mysqli_query($con,$sql)) { 
    echo 'Table "tblStudents" created successfully!'; 
    } else { 
    echo 'Error creating table "tblStudents"'; 
    } 
} 

?>; 
+1

'CREATE TABLE tblStudents IF NOT EXISTS'。 http://dev.mysql.com/doc/refman/5.1/en/create-table.html –

+0

在'level TINYINT NOT NULL'之後移除','' – Gautam3164

+1

您可能需要引用索引,因爲它是一個sql保留字 –

回答

4

你有一些錯誤,這裏是正確的說法

CREATE TABLE `table_name` 
(
    `index` INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(`index`), 
    fullName CHAR(30) NOT NULL, 
    dateOfBirth DATE NOT NULL, 
    SID TINYINT NOT NULL, 
    address CHAR(30) NOT NULL, 
    level TINYINT NOT NULL 
) 

你不得不進行的錯誤:

  • 指數是一個保留關鍵字,以便使用反引號`index`爲列名稱
  • 「level TINYINT NOT NULL」後面有一個額外的逗號(,)。
+0

謝謝。它解決了問題 –

+0

很高興幫助你! –

1

爲字段'index'提供另一個名稱。你不能使用它,因爲它是保留字。

0

index這裏是保留關鍵字。

所以,你需要添加一個傾斜(`)來索引。

更正代碼:

CREATE TABLE tblStudents 
(
    `index` INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(`index`), 
    fullName CHAR(30) NOT NULL, 
    dateOfBirth DATE NOT NULL, 
    SID TINYINT NOT NULL, 
    address CHAR(30) NOT NULL, 
    level TINYINT NOT NULL 
) 
0
**Try below code,I think it will work fine..** 




<?php 

    $con=mysqli_connect("localhost","root","noor.xbyte","fathis_quran_class"); 
    if (mysqli_connect_errno()) { 
     echo '&lt;h1>Error Connecting to the database!&lt;/h1>'; 
    } else { 
     $sql = "CREATE TABLE tblStudents(

     index int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY (`index`), 
     fullName varchar(30) NOT NULL, 
     dateofbirth date NOT NULL, 
     SID tinyint(4) NOT NULL, 
     address varchar(30) NOT NULL, 
     level tinyint(4) NOT NULL); 
     if (mysqli_query($con,$sql)) { 
     echo 'Table "tblStudents" created successfully!'; 
     } else { 
     echo 'Error creating table "tblStudents"'; 
     } 
    } 

    ?>;