0

我正在尝试使用 PHP 和 MySQL 创建一个表。第一个表不会在数据库上创建,但第二个会。我认为这是我的参数/约束。这是我的代码:

if ($conn==true){
     $tablefriends = "CREATE TABLE IF NOT EXISTS friends (
        friend_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        friend_email VARCHAR(64) NOT NULL PRIMARY KEY ,
        password VARCHAR(16) NOT NULL ,
        profile_name VARCHAR(32) NOT NULL ,
        date_started DATE NOT NULL ,
        num_of_friends INT UNSIGNED ZEROFILL NULL default '0'
        );";

    $tablemyfriends = "CREATE TABLE IF NOT EXISTS myfirends ( 
        friend_id1 INT NOT NULL , 
        friend_id2 INT NOT NULL 
        );";

    $resulttf = @mysqli_query($conn, $tablefriends);
    if($resulttf==false){
        echo "<p>Failed to create friends table</p>";
        }
    $resulttmf = @mysqli_query($conn, $tablemyfriends);
    if($resulttmf==false){
        echo "<p>Failed to create myfriends table</p>"; 
        }else{

    echo"<p>Tables successfully created</p>";
        }
    mysqli_close($conn);
}

else {
    echo "<p>Failed to connect</p>";
    }

不知道为什么导师说要分两次入小学

4

4 回答 4

5

2主键friend_idfriend_email

在将其放入PHP之前,请务必检查您的查询。

于 2012-10-29T10:19:34.243 回答
1

在您的第一个查询($tablefriends)中,您定义了两个主键。希望下面的代码对你有用。

 if ($conn==true){
         $tablefriends = "CREATE TABLE IF NOT EXISTS friends (
            friend_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
            friend_email VARCHAR(64) NOT NULL ,
            password VARCHAR(16) NOT NULL ,
            profile_name VARCHAR(32) NOT NULL ,
            date_started DATE NOT NULL ,
            num_of_friends INT (64) UNSIGNED ZEROFILL NULL default '0'
            );";

        $tablemyfriends = "CREATE TABLE IF NOT EXISTS myfirends ( 
            friend_id1 INT NOT NULL , 
            friend_id2 INT NOT NULL 
            );";

        $resulttf = @mysqli_query($conn, $tablefriends);
        if($resulttf==false){
            echo "<p>Failed to create friends table</p>";
            }
        $resulttmf = @mysqli_query($conn, $tablemyfriends);
        if($resulttmf==false){
            echo "<p>Failed to create myfriends table</p>"; 
            }else{

        echo"<p>Tables successfully created</p>";
            }
        mysqli_close($conn);
    }

    else {
        echo "<p>Failed to connect</p>";
        }
于 2012-10-29T10:25:24.037 回答
0

授予运行查询的用户的 GRANTS 是什么?也许用户只能创建第二个表。

查看 MySQL 日志文件,看看是否有任何错误消息可以帮助您。

于 2012-10-29T10:23:21.400 回答
0
if ($conn==true){
     $tablefriends = "CREATE TABLE IF NOT EXISTS friends (
        friend_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        friend_email VARCHAR(64) NOT NULL,
        password VARCHAR(16) NOT NULL ,
        profile_name VARCHAR(32) NOT NULL ,
        date_started DATE NOT NULL ,
        num_of_friends INT UNSIGNED ZEROFILL NULL default '0'
        );";

    $tablemyfriends = "CREATE TABLE IF NOT EXISTS myfirends ( 
        friend_id1 INT NOT NULL , 
        friend_id2 INT NOT NULL 
        );";

    $resulttf = @mysqli_query($conn, $tablefriends);
    if($resulttf==false){
        echo "<p>Failed to create friends table</p>";
        }
    $resulttmf = @mysqli_query($conn, $tablemyfriends);
    if($resulttmf==false){
        echo "<p>Failed to create myfriends table</p>"; 
        }else{

    echo"<p>Tables successfully created</p>";
        }
    mysqli_close($conn);
}

else {
    echo "<p>Failed to connect</p>";
    }
于 2012-10-29T10:24:43.613 回答