如果注册名称已经存在,我想停止用户注册。
这是我的代码:
<?php
$username = filter_input(INPUT_POST, 'name');
$password = md5(filter_input(INPUT_POST, 'password'));
print_r($_POST['name']);
if (empty($username)){
echo "Username should not be empty"; die();
}
if (empty($password)){
echo "Password should not be empty"; die();
}
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "db_account";
//create connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
// Check the database for duplicate username
$username_check_query = "SELECT * FROM t_account WHERE name='$username' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$username = mysqli_fetch_assoc($result);
if ($username) { // if user exists
if ($username['name'] === $username) {
echo "Username already exists"; die();
}
}
// Register user
if (mysqli_connect_error()) {
die('Connect Error ('. mysqli_connect_error() .') ' . mysqli_connect_error());
} else {
$sql = "INSERT INTO t_account (name, pwd)
VALUES('$username', '$password')";
if ($conn->query($sql)){
echo " Account created successfully!";
}
else{
echo "Error: ". $sql."<br>". $conn->error;
}
$conn->close();
}
}
?>
我仍然是新手,所以我不知道它是否正确。我使用了模板中的一部分并进行了编辑。现在,当我尝试注册时,它会给我Error 500..
答案 0 :(得分:0)
<form action="" method="post">
<input type="text" name="name">
<input type="text" name="password">
<button type="submit">send</button>
</form>
<?php
$username = filter_input(INPUT_POST, 'name');
$password = md5(filter_input(INPUT_POST, 'password'));
print_r($_POST['name']);
if (empty($username)){
echo "Username should not be empty"; die();
}
if (empty($password)){
echo "Password should not be empty"; die();
}
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "db_account";
//create connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
// Check the database for duplicate username
$username_check_query = "SELECT * FROM t_account WHERE name='$username' LIMIT 1";
$result = mysqli_query($conn, $username_check_query);
$username_from_db = mysqli_fetch_assoc($result);
if ($username_from_db) { // if user
if ($username_from_db['name'] === $username) {
echo "Username already exists"; die();
}
}
// Register user
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_error() .') '
. mysqli_connect_error());
} else{
$sql = "INSERT INTO t_account (name, pwd)
VALUES('$username', '$password')";
if ($conn->query($sql)){
echo " Account created successfully!";
}
else{
echo "Error: ". $sql."<br>". $conn->error;
}
$conn->close();
}
?>