This code works and adds tables to my database. My question is how do I protect it with prepared statements.
require "conn.php";
$MyServer =($_POST["username"]);
$sql = ("CREATE TABLE $MyServer (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL
)");
if($conn->query($sql) === TRUE){
echo "Table created successfully";
}
I am using MySQLi. I tryed this and it isn't adding the table.
$MyServer =($_POST["username"]);
if (!preg_match('^/[A-Za-z][A-Za-z0-9]{0,7}$/', $MyServer)) {
throw new Exception ('username unsuitable for use as a table name');
}
$sql = ("CREATE TABLE `$MyServer` (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL
)");
if($conn->query($sql) === TRUE){
echo "Table created successfully";
} else {
echo "Table is not created successfully ";
}