此代碼應檢查用戶是否在表中,並且是否爲用戶創建表。現在它只是命名錶$ MyServer。我如何使它成爲名稱是變量$ MyServer。在PHP中爲某些變量創建表(MySQLi)
<?php
require "conn.php";
echo "debug 1";
$stmt = $conn->prepare("SELECT * FROM UserData WHERE username = ?");
$stmt->bind_param('s',$username);
$username = $_POST["username"];
$stmt->execute();
$stmt->store_result();
echo "debug 2";
if ($stmt->num_rows == 0){ // username not taken
echo "debug 3";
$stmt2 = $conn->prepare("INSERT INTO UserData (username, password) VALUES (?, ?)");
$password =($_POST["password"]);
$username =($_POST["username"]);
$stmt2->bind_param('ss', $username, $password);
$stmt2->execute();
$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";
} else {
echo "Table is not created successfully ";
}
if ($stmt2->affected_rows == 1){
echo 'Insert was successful.';
}else{ echo 'Insert failed.';
var_dump($stmt2);
}
}else{ echo 'That username exists already.';}
?>
如果要爲每個用戶創建一個表,你需要評估你的數據結構,這通常是一個非常糟糕的主意。 – Difster
另請注意,您對SQL注入攻擊廣泛開放, –
我將如何保護自己 –