Php 使用由变量指定的默认值创建表
下面我有一个php脚本,它创建了一个表。该表是根据用户电子邮件的名称创建的。但是当创建表时,由变量$firstname指定的first_name的默认值为空。事实上,所有字段都是空白的。我的剧本有什么问题吗Php 使用由变量指定的默认值创建表,php,Php,下面我有一个php脚本,它创建了一个表。该表是根据用户电子邮件的名称创建的。但是当创建表时,由变量$firstname指定的first_name的默认值为空。事实上,所有字段都是空白的。我的剧本有什么问题吗 $email = $_SESSION['email']; $firstname = $user['first_name']; // Create database for user if not exists $DB_CON->exec("CREATE TABLE IF NOT EX
$email = $_SESSION['email'];
$firstname = $user['first_name'];
// Create database for user if not exists
$DB_CON->exec("CREATE TABLE IF NOT EXISTS `".$email."` (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(100) NOT NULL DEFAULT '.$firstname.'
PRIMARY KEY (`id`)
)");
基本上,这不是数据库的工作方式,而是针对您的问题:
$email = $_SESSION['email'];
$firstname = $user['first_name'];
// Create database for user if not exists
$DB_CON->exec("CREATE TABLE IF NOT EXISTS `".$email."` (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(100) NOT NULL DEFAULT ".$firstname."
PRIMARY KEY (`id`)
);INSERT INTO `".$email."` (`id, `first_name`) VALUES(NULL, NULL)");
但正如我所说,数据库不是这样工作的
您最可能希望创建一个users表并使用它:
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(100) NOT NULL,
`first_name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`))
然后在此表中插入数据
DB_CON->exec("INSERT INTO `users` (`email`, `first_name`) VALUES (".$email.", ".firstname."")
你确定要这样做吗?听起来这是一个两步的过程,很有意义。