计划说明:
我想要构建的程序是使用 php 在 mysql 中创建一个表。表名是从前一页的表单在文本框中输入的值。按钮名称为create
。文本框名称为table_name
。
我不确定mysql_select_db('db_studentinfo', $con)
是否会产生问题,或者我在 $ sql 中编写表格列的方式。
我遇到问题(创建表时出错: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 '2014( ID_Number INT NOT NULL, PRIMARY KEY (ID_Number), First_Name VARCH' at line 1'
)
请帮忙。
<?php
session_start();
$con = mysqli_connect("localhost","root","","db_studentinfo");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['create'])){
$filename = $_POST['table_name'];
echo "Connected successfully";
mysql_select_db('db_studentinfo', $con);
$sql = "CREATE TABLE $filename(
ID_Number INT NOT NULL,
PRIMARY KEY (ID_Number),
First_Name VARCHAR(50),
Middle_Name VARCHAR(50),
Last_Name VARCHAR(50),
Year INT,
Course VARCHAR(50),
Position VARCHAR(50),
Party VARCHAR(50),
Picture VARCHAR(50),
vote INT,
)";
if (mysqli_query($con, $sql)){
echo "Table $filename created successfully";
}
else{
echo "Error creating table: " . mysqli_error($con);
}
mysqli_close($con);
}
?>
答案 0 :(得分:1)
您需要删除create statement
中最后一列之后的逗号vote INT,
答案 1 :(得分:0)
字符串连接错误。试试这个 -
$sql = "CREATE TABLE $filename(".
"ID_Number INT NOT NULL,".
"PRIMARY KEY (ID_Number),".
"First_Name VARCHAR(50),".
"Middle_Name VARCHAR(50),".
"Last_Name VARCHAR(50),".
"Year INT,".
"Course VARCHAR(50),".
"Position VARCHAR(50),".
"Party VARCHAR(50),".
"Picture VARCHAR(50),".
"vote INT".
")";
答案 2 :(得分:0)
(创建表时出错:您的SQL语法出错;请查看与您的MySQL服务器版本对应的手册,以便在' 2014附近使用正确的语法( ID_Number INT NOT NULL,PRIMARY KEY(ID_Number),第1行的First_Name VARCH')
除了其他可能的错误(如其他答案中所述),上面的错误消息专门返回,因为您尝试创建一个名为 2014 的表,这是不允许的。
“标识符可以以数字开头,但除非引用可能不仅仅由数字组成”。 [MySql Manual : 9.2 Schema Object Names]
您可以使用反引号转义仅数字表名:
CREATE TABLE `2014`
....