Haris Irshad Haris Irshad - 7 months ago 10
PHP Question

Sqli not creating tables in PHP code

I am trying to see where the syntax is incorrect but this code won't make the following two tables, however, will make other small tables whose code I found online, such as

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


But it won't make the two tables defined below in php code. Also,
$mysqli
is the database connection object defined in other class that is required here.

<?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();

?>


Also for some reason, the following code that i found online works:

$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);

Answer

You might try pasting the code into MySQL, if you are unable to configure logging correctly on your web server. You'd find that varchar is not a valid MySQL data type. Try VARCHAR(255) instead.

Comments