简体   繁体   中英

mySQL syntax error when creating simple table

I recently switched from Linux to Windows for work, and the way that I used to create tables through php in mysql doesn't seem to work anymore.

I first tried to create the table using some php variables:

$tablename=$_POST['tablename'];
$fields=array('First Name', 'Last Name', 'Institution', 'Abbr.', 'City', 'Country', 'Phone', 'Email', 'Found by', 'Salesperson', 'Temp', 'Notes');

$sql ="CREATE TABLE $tablename
  (
  PID INT NOT NULL AUTO_INCREMENT,";
foreach ($fields as $field){$sql .= " $field CHAR(50),";}
$sql .= "PRIMARY KEY(PID))";

if (mysqli_query($con,$sql))
  {echo "Table persons created successfully";}
else
  {echo "Error creating table: " . mysqli_error($con);}

Which returned the error: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( PID INT NOT NULL AUTO_INCREMENT, FirstName NOT NULL CHAR(50), LastNam' at line 2"

I thought something might be up with the spaces, so then I tried inputting the table data manually:

$sql="CREATE TABLE $tablename
  (
  PID INT NOT NULL AUTO_INCREMENT,
  FirstName NOT NULL CHAR(50),
  LastName NOT NULL CHAR(50),
  Institution NOT NULL CHAR(50),
  Abbr NOT NULL CHAR(50),
  City NOT NULL CHAR(50),
  Country NOT NULL CHAR(50), 
  Phone NOT NULL CHAR(50), 
  Email NOT NULL CHAR(50), 
  Foundby NOT NULL CHAR(50),
  Salesperson NOT NULL CHAR(50),
  Temp NOT NULL CHAR(50),
  Notes NOT NULL CHAR(50),
  PRIMARY KEY(PID))";

Bu that returned the same error message. I have tried my best to modify this every which way I could based on previous suggestions but I am stuck. A table of almost exactly the same format as the second example works perfectly on my Linux box...

NOT NULL should follow column type

For example it should be :

FirstName CHAR(50) NOT NULL

But you put it as :

FirstName NOT NULL CHAR(50)

This worked for me :

CREATE TABLE test_table
(
    PID INT NOT NULL AUTO_INCREMENT,
    FirstName  CHAR(50) NOT NULL,
    LastName CHAR(50) NOT NULL,
    Institution CHAR(50) NOT NULL,
    Abbr CHAR(50) NOT NULL,
    City CHAR(50) NOT NULL,
    Country CHAR(50) NOT NULL, 
    Phone CHAR(50) NOT NULL, 
    Email CHAR(50) NOT NULL, 
    Foundby CHAR(50) NOT NULL,
    Salesperson CHAR(50) NOT NULL,
    Temp CHAR(50) NOT NULL,
    Notes CHAR(50) NOT NULL,
    PRIMARY KEY(PID)
)

Your code looks fine to generate the SQL for CREATE TABLE , but as your column names have spaces , you would need to use back ticks :

$tablename = 'persons';

$fields = array('First Name', 'Last Name', 'Institution', 'Abbr.', 'City', 'Country', 'Phone', 'Email', 'Found by', 'Salesperson', 'Temp', 'Notes');

$sql = "CREATE TABLE `".$tablename."`
(
    `PID` INT NOT NULL AUTO_INCREMENT,";
foreach( $fields as $field )
{
    $sql .= "
    `" .$field ."` CHAR(50) NOT NULL,";
}
$sql .= "
    PRIMARY KEY(PID)
)";

echo $sql;

Will give :

CREATE TABLE `persons`
(
    `PID` INT NOT NULL AUTO_INCREMENT,
    `First Name` CHAR(50) NOT NULL,
    `Last Name` CHAR(50) NOT NULL,
    `Institution` CHAR(50) NOT NULL,
    `Abbr.` CHAR(50) NOT NULL,
    `City` CHAR(50) NOT NULL,
    `Country` CHAR(50) NOT NULL,
    `Phone` CHAR(50) NOT NULL,
    `Email` CHAR(50) NOT NULL,
    `Found by` CHAR(50) NOT NULL,
    `Salesperson` CHAR(50) NOT NULL,
    `Temp` CHAR(50) NOT NULL,
    `Notes` CHAR(50) NOT NULL,
    PRIMARY KEY(PID)
)

The above SQL created table without any issues .

Your FirstName NOT NULL CHAR(50) and all the lines after that is written in the wrong order.

It should be {column_name} {datatype} {options}, but you have the {datatype} and the {options} in the other way around.

So fix everything to FirstName CHAR(50) NOT NULL and it should work.

(Your PID is in the correct format)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM