我正在练习 PHP 和 SQL。我有一个注册表单,如果用户的电子邮件已经注册,我想阻止他们在我的网站上注册。我的代码没有这样做,我仍然看不到我的错误。有人能帮助我吗?这是我的 php 文件。
$sql_user_name = "root"; //PHPMYADMIN INFORMATIONS
$sql_password = "";
$sql_database = "registeredusers";
$sql_table = "registeredusers";
$server = "127.0.0.1";
$db_handle = mysqli_connect($server, $sql_user_name, $sql_password); //CONNECTION TO DATABASE
$db_found = mysqli_select_db($db_handle,$sql_database); //OPENING TABLE
$email_exists = mysqli_query("SELECT * FROM $sql_table WHERE email = '$email'");
if ($db_found && mysqli_num_rows($email_exists) > 0) {
header('Location: index.php');
echo "Email is already registered.";
mysqli_close($db_handle);
}
else if ($db_found) { //WRITES INTO DATABASE IF TABLE HAS SUCCESSFULLY BEEN OPENED AND EMAIL ISN'T IN USE
$SQL = mysqli_query($db_handle,"INSERT INTO registeredusers (name, email, password, gender, birthday)
VALUES ('$name', '$email', '$password', '$gender', '$birthday')");
$to = "$email";
$subject = "Welcome to Sketchule";
$message = "Hello! Welcome to Sketchule!";
$from = "lpb.baillargeon@gmail.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
mysqli_close($db_handle);
header('Location: confirmation.php');
}
else { //FINISHED CONNECTION IF OPERATION UNSUCCESSFUL
mysqli_close($db_handle);
}
更新:
它永远不会进入脚本的这一部分:
if ($db_found && mysqli_num_rows($email_exists) > 0) {
$_SESSION['registered'];
mysqli_close($db_handle);
header('Location: index.php');
exit();
}
它永远不会将我重定向到 index.php,它仍然会发送一封电子邮件。
更新:这就是我让它工作的方式。
$query = "SELECT * FROM registeredusers WHERE email = '$email'";
$result = mysqli_query($db_handle, $query) or die (mysqli_error($db_handle));
if (mysqli_num_rows($result) > 0) {
session_start();
$_SESSION['registered'] = "Email is already registered.";
mysqli_close($db_handle);
header('Location: index.php');
exit();
}
else { //WRITES INTO DATABASE IF TABLE HAS SUCCESSFULLY BEEN OPENED AND EMAIL ISN'T IN USE
$SQL = mysqli_query($db_handle,"INSERT INTO registeredusers (name, email, password, gender, birthday)
VALUES ('$name', '$email', '$password', '$gender', '$birthday')");
$to = "$email";
$subject = "Welcome to Sketchule";
$message = "Hello! Welcome to Sketchule!";
$from = "lpb.baillargeon@gmail.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
mysqli_close($db_handle);
header('Location: confirmation.php');
exit();
}