使用php创建数据表时遇到问题,我尝试使用读入的名称创建表,这是我工作的代码部分:
<?php
include 'db-info-information.php';
$connection = mysqli_connect($dbhost, $dbuser, $dbpass);
if (! $connection ){
die('Could not connect!');
}
$name = Trim(stripslashes($_POST['name']));
$phone = Trim(stripslashes($_POST['phone']));
$parent = Trim(stripslashes($_POST['parent']));
$parent_phone = Trim(stripslashes($_POST['parent-phone']));
mysqli_select_db($connection,"info");
$sql="create table '$name' (
name varchar(20),
phone int,
parent varchar(20),
parent_phone int
)";
if(mysqli_query($connection, $sql))
{
header("Location: success.php");
}
else
{
echo "error";
}
$ins = "insert into '$name' (name, phone, parent, parent_phone) values ('$name','$phone', '$parent','$parent_phone')";
if(mysqli_query($connection, $ins))
{
header("Location: success.php");
}
mysqli_close($connection);
?>
我试图用$ name的名称命名新表,那么我应该在$ ___ _ = _#34;创建表____(?
答案 0 :(得分:0)
这是一个单引号:'
这是一个反击:`
在美国键盘上,反引号位于顶行“1”的左侧。
将反引号包围的$ name放回一个引号:
$sql="create table `$name` (
name varchar(20),
phone int,
parent varchar(20),
parent_phone int
)";
如果您要使用任何键/保留字,则使用反引号,因此并非总是需要。
您可能会发现从位于此处的MySQL文档中了解有关CREATE TABLE
的更多信息非常有用:
http://dev.mysql.com/doc/refman/5.7/en/create-table.html
更新
您的插入查询不应该在表上使用单引号,它必须使用反引号:
$ins = "insert into `$name` (name, phone, parent, parent_phone) values ('$name','$phone', '$parent','$parent_phone')";