FYI new to coding, self-taught, go easy..
I have a registration form that creates a new SQL table. The company name they input becomes the table name of a newly created table for each login.
Everything is working great except there is a vulnerability in the variable of the table name.
I am using PHP and MySQL.
I originally had issues if the company name had a space in between 2 words, but I fixed that using str_replace
I was testing a lot and found that if I put a company name in "out", it breaks the code.
I receive this error message:
"Fatal error: Uncaught mysqli_sql_exception: 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 'out ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(128) NOT ' at line 1 in C:\Users\reece.grover\OneDrive\XML Website\opregister.php:73 Stack trace: #0 C:\Users\reece.grover\OneDrive\XML Website\opregister.php(73): mysqli->query('CREATE TABLE ou...') #1 {main} thrown in C:\Users\reece.grover\OneDrive\XML Website\opregister.php on line 73"
This seems open to not only errors but also SQL injection.
I learned how to bind_param but that still stops the potential code-breaking of the CREATE TABLE function.
Here is my server-side PHP code, the form is just a simple HTML form with Bootstrap.
//remove spaces
$company = str_replace(' ', '', $_POST["company"]);
//hash Password
$password_hash = password_hash($_POST["password"], PASSWORD_DEFAULT);
// Create new Operator
$mysql = require __DIR__ . "/database.php";
$sql = "INSERT INTO operators (first_name, last_name, email, password_hash, company)
VALUES (?, ?, ?, ?, ?)";
$stmt = $mysql->stmt_init();
if ( ! $stmt ->prepare($sql)){
die("SQL error: " . $mysql->error);
}
$stmt->bind_param("sssss",
$_POST["first_name"],
$_POST["last_name"],
$_POST["email"],
$password_hash,
$company);
if ( ! $stmt->execute()) {
die($mysqli->error . " " . $mysqli->errno);
}
//Create the new company table
$sql = "CREATE TABLE $company (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(128) NOT NULL,
last_name VARCHAR(128) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
company VARCHAR(128),
credit VARCHAR(60),
phone VARCHAR(60))";
if ( ! $mysql->query($sql)) {
die("Create Table Failed : " . $mysql->error);
}
$sql = "INSERT INTO $company (first_name, last_name, email, company, credit, phone)
VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $mysql->stmt_init();
if ( ! $stmt ->prepare($sql)){
die("SQL error: " . $mysql->error);
}
$stmt->bind_param("ssssss",
$_POST["first_name"],
$_POST["last_name"],
$_POST["email"],
$company,
$_POST["credit"],
$_POST["phone"]);
if ($stmt->execute()) {
header("Location: signup_success.php");
exit;