What I've looked at already
I'm seriously new to PHP and I was hoping you guys could point me in the right direction. I've read the following threads and adapted its answers, but I'm obviously not doing something correctly:
Creating table with variable name php mysql
Use a variable as table name when creating a table in mysql
Create mysql table with php variable not working
In particular, its this block of code I've been trying to adapt to my own needs as it was one of the few examples that used heredocs, which is what we've been taught to use:
$create = <<<SQL
CREATE TABLE `$tableusername` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`please` VARCHAR(50) NOT NULL,
`make` VARCHAR(50) NOT NULL,
`this` VARCHAR(50) NOT NULL,
`work` VARCHAR(50) NOT NULL
)
My situation
I want my register.php to create a new table using the username the user has chosen. (Register.php isn't accessible if a user is already logged in, so duplicates of a table will never be possible.) This table will later be used to graph the data contained within the table, which is why I've not set a PK, as the table will never be joined or have its contents shared with another table. (If it turns out this is what's not making it work, I'll add in an auto-incrementing ID).
The problem
The table doesn't get created.
My register.php EDIT: In accordance with a suggestion, I've added to the code
I got an error message pertaining to the syntax of encapsulating each column name within quotes, so I removed those to then get the following error message:
Could not query database1064 : 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 ')' at line 11
<?php
include('template.php');
if (isset($_POST['username']) and isset($_POST['password'])) {
$name = $mysqli->real_escape_string($_POST['username']);
$pwd = $mysqli->real_escape_string($_POST['password']);
$mail = $mysqli->real_escape_string($_POST['email']);
$first = $mysqli->real_escape_string($_POST['fname']);
$last = $mysqli->real_escape_string($_POST['lname']);
$query = <<<END
INSERT INTO p_users(username,password,email,fname,lname)
VALUES('{$name}','{$pwd}','
{$mail}','{$first}','{$last}')
END;
$table = <<<END
CREATE TABLE `$name` (
steps_per_day int,
energy_expenditure int,
distance_walked_per_day int,
bed_time int,
waking_time int,
hours_slept int,
num_daytime_naps int,
outdoor_temp int,
indoor_temp int
)
END;
if ($mysqli->query($table) !== TRUE) {
die("Could not query database" . $mysqli->errno . " : " . $mysqli->error);
header('Location:index.php');
}
if ($mysqli->query($query) !== TRUE) {
die("Could not query database" . $mysqli->errno . " : " . $mysqli->error);
header('Location:index.php');
}
}
$content = <<<END
<form method="post" action="register.php">
<input type="text" name="username" placeholder="username"><br>
<input type="password" name="password" placeholder="password"><br>
<input type="text" name="email" placeholder="email"><br>
<input type="text" name="fname" placeholder="first name"><br>
<input type="text" name="lname" placeholder="last name"><br>
<input type="submit" value="Register">
<input type="Reset" value="reset">
</form>
END;
echo $navigation;
echo $content;
?>