我正在尝试使用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>";
}
我不知道为什么导师说要把两次放入小学
答案 0 :(得分:5)
2 Primary keys 。 friend_id
和 friend_email
在将查询放入 PHP 之前,请务必检查您的查询。
答案 1 :(得分: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>";
}
答案 2 :(得分:0)
为运行查询的用户提供了哪些授权?也许用户只能创建第二个表。
查看MySQL日志文件,看看是否有任何可以帮助您的错误消息。
答案 3 :(得分: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>";
}