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`)
)");
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."")