要創(chuàng)建用戶表并添加數(shù)據(jù),可以按照以下步驟進行:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table users created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$username = "JohnDoe";
$email = "johndoe@example.com";
$password = "password123";
$sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
以上是一個簡單的示例,你可以根據(jù)自己的需求對表結(jié)構(gòu)和數(shù)據(jù)進行更改。在實際應(yīng)用中,建議使用預(yù)處理語句來防止SQL注入攻擊。