doudu7626 2017-05-08 01:23
浏览 29
已采纳

使用变量指定的默认值创建表

I have a php script below that creates a table. The table is created with a name according to the user email. But when the table is created, the default value for first_name which is assigned by the variable $firstname is blank. In fact, all the fields are blank. Are there any wrong with my script?

$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`)
 )");
  • 写回答

1条回答 默认 最新

  • dongluan1901 2017-05-08 01:40
    关注

    Basically thats not how Databases work but for your Problem:

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

    But as I said, thats not how databases work...

    Moste likely you want to create a users table and work with that:

    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`))
    

    And then insert your data in this table.

    DB_CON->exec("INSERT INTO `users` (`email`, `first_name`) VALUES (".$email.", ".firstname."")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C语言设计一个简单的自动换档程序
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。