我正在使用mysql和php为我的网站构建一个登录系统。我想确保用户没有相同的用户名。
<?php
$servername = "xxxxxxxx";
$username = "xxxxxxx";
$password = "xxxxxxx";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//pulls variables from the html form
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$date=$POST['date'];
//inserts
$sql = "INSERT INTO users (username, password, email, fname, lname, birthday)
VALUES ('$username', '$password', '$email', '$fname', '$lname', '$date')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
如果有人试图使用用户名&#34; test123&#34;创建一个帐户,我想这样做。和&#34; test123&#34;已经被使用然后会出现一个页面告诉该人选择不同的用户名。