使用MySQL和PHP创建表的问题

时间:2014-11-24 13:21:05

标签: php sql mysqli

我正在尝试在索引页面上创建一个数据库,如果数据库不存在,它将自动创建一个数据库,然后创建一个表并向其中添加数据。

我遇到了创建表格的问题,它总会返回:

  

无法创建表格

它创建数据库没有问题,它只是导致问题的表。谁知道我哪里错了?

<?php

$servername = "localhost";
$username = "root";
$password = "admin";
$database = "jfitness";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS '($database)'";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully";
} else {
    echo "Database already exists: " . mysqli_error($conn);
}

// make foo the current db
$db_selected = mysqli_select_db($conn, $database);
if (!$db_selected) 
    {
    die ('Can\'t use database : ' . mysqli_error());
}
else
{
    echo "Database Selected ";
}

// sql to create table
$sql = "CREATE TABLE customers( ".
       "id INT NOT NULL AUTO_INCREMENT, ".
       "name VARCHAR(40) NOT NULL, ".
       "password VARCHAR(50) NOT NULL, ".
       "email VARCHAR(50 NOT NULL), ".
       "PRIMARY KEY ( id )); ";

$retval = mysqli_query($conn, $sql );
if(! $retval )
{
  die('Could not create table: ' . mysqli_error());
}
echo "Table created successfully\n";

$sql = "INSERT INTO customers (username, password, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

mysqli_close($conn);

?>

5 个答案:

答案 0 :(得分:4)

在create table中取消长度声明之外的NOT NULL

email VARCHAR(50 NOT NULL) // its inside the length

其次,您的列不匹配:

INSERT INTO customers (username,    // username/name
name VARCHAR(40) NOT NULL

把它变成这个:

CREATE TABLE customers( 
    id INT NOT NULL AUTO_INCREMENT, 
    username VARCHAR(40) NOT NULL, // username not name
    password VARCHAR(50) NOT NULL, 
    email VARCHAR(50) NOT NULL, // put it after that
    PRIMARY KEY (`id`)
)

你有一个die('Could not create table: ' . mysql_error());。它不属于那里。

总而言之:

$servername = "localhost";
$username = "root";
$password = "admin";
$database = "jfitness";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $database";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully <br/>";
} else {
    echo "Database already exists: " . mysqli_error($conn);
}

// make foo the current db
$db_selected = mysqli_select_db($conn, $database);
if (!$db_selected) 
    {
    die ('Can\'t use database : ' . mysqli_error($conn));
}
else
{
    echo "Database Selected <br/>";
}

// sql to create table
$sql = "CREATE TABLE customers( 
    id INT NOT NULL AUTO_INCREMENT, 
    username VARCHAR(40) NOT NULL, 
    password VARCHAR(50) NOT NULL, 
    email VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`)
)";


$retval = mysqli_query($conn, $sql );
if(! $retval ) {
    die('Could not create table: ' . $conn->error);
}

echo "Table created successfully<br/>";

$sql = "INSERT INTO customers (username, password, email) VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully <br/>";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

mysqli_close($conn);

答案 1 :(得分:0)

选择以下行:

email VARCHAR(50 NOT NULL)

并将其更改为:

email VARCHAR(50) NOT NULL

答案 2 :(得分:0)

你的sql语句错误

在这一行:

"email VARCHAR(50 NOT NULL), ".

试试这个:

"email VARCHAR(50) NOT NULL, ".

答案 3 :(得分:0)

 "email VARCHAR(50 NOT NULL), ".

应该是

 "email VARCHAR(50) NOT NULL, ".

答案 4 :(得分:0)

您在表格语法中定义了电子邮件字段时出现语法错误。您可以使用以下完整解决方案来解决您的问题:

$servername = "localhost";
$username = "root";
$password = "admin";
$database = "jfitness";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $database";
if (mysqli_query($conn, $sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Database already exists: " . mysqli_error($conn);
}

// make foo the current db
$db_selected = mysqli_select_db($conn, $database);
if (!$db_selected) 
    {
    die ('Can\'t use database : ' . mysqli_error());
}
else
{
    echo "Database Selected ";
}

// sql to create table
$sql = "CREATE TABLE customers( ".
       "id INT NOT NULL AUTO_INCREMENT, ".
       "username VARCHAR(40) NOT NULL, ".
       "password VARCHAR(50) NOT NULL, ".
       "email VARCHAR(50) NOT NULL, ".
       "PRIMARY KEY ( id )); ";

$retval = mysqli_query($conn, $sql );
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table created successfully\n";