Sqli没有在PHP代码中创建表

时间:2016-04-18 19:55:50

标签: php sql mysqli

我正在尝试查看语法不正确的地方,但是这段代码不会生成以下两个表,但是,我会在其他小表中找到我在网上找到的代码,例如

CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

但它不会在PHP代码中制作下面定义的两个表。也, $mysqli是此处所需的其他类中定义的数据库连接对象。

<?php // Check connection
    $connectionStatus = FALSE;
    if (!$mysqli) {
       echo "<h2>Unable to connect to MySqli database!</h2>";
    } 
    else{
        echo "<h2>Successfully connected to MySqli database!</h2>";
        $connectionStatus = TRUE;
    }
    if($connectionStatus == TRUE){
         echo "<form action='' method='POST'>"; 
            echo "FLATFILE: <input type='file' name='FlatFile' /><br/><br/>";
            echo "<input type='submit'/>";
            echo "</form>";
    }
    if($connectionStatus == TRUE){
        $sql = "CREATE TABLE Supplier(
        Code varchar(255),
        Name varchar,
        Address1 varchar,
        Address2 varchar, 
        City varchar, 
        State varchar,
        Zip varchar,
        Country varchar,
        Phone varchar,
        Fax varchar,
        PRIMARY KEY (Code))";
        mysqli_query($mysqli, $sql);

            $sql = "CREATE TABLE PO(
        Number varchar(255),
        ReceivingFactoryCode varchar,
        SupplierCode varchar,
        IssueYear varchar, 
        IssueMonth varchar, 
        IssueDay varchar,
        BuyingFactoryCode varchar,
        PRIMARY KEY (Number),
        FOREIGN KEY (ReceivingFactoryCode) REFERENCES Supplier(Code)
        )";
        mysqli_query($mysqli, $sql);



    }


    $mysqli->close();

    ?>

出于某种原因,我在网上找到的以下代码也适用:

$sql = "CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)";
    mysqli_query($mysqli, $sql);

 $sql = "CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)";
mysqli_query($mysqli, $sql);

1 个答案:

答案 0 :(得分:0)

如果您无法在Web服务器上正确配置日志记录,则可以尝试将代码粘贴到MySQL中。您会发现varchar不是有效的MySQL数据类型。请改为VARCHAR(255)