我正在练习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();
}
答案 0 :(得分:5)
更好的方法是,您需要在SESSION
中存储邮件,然后在重定向的页面上回显它,还必须在exit()
之后使用header()
...
if (mysqli_num_rows($email_exists) > 0) {
$_SESSION['msg'] = "Email is already registered.";
http_redirect('index.php', [], TRUE);
}
然后在index.php上
echo $_SESSION['msg'];
答案 1 :(得分:3)
在数据库中,将电子邮件ID列设为UNIQUE。这应该解决它。当有人尝试使用相同的ID注册时,请创建相应的错误消息。
答案 2 :(得分:2)
header('Location: index.php');
语句会在处理echo "Email is already registered.";
之前重定向您的网页。
因此,为了看到显示的错误消息重定向到其他页面,例如header('Location: error.php');
error.php
显示错误消息,或者删除header('Location: index.php');
语句
答案 3 :(得分:1)
在echo之后删除header()函数,您的消息将可见。
`if($ db_found&& mysqli_num_rows($ email_exists)> 0){
echo "Email is already registered.";
mysqli_close($db_handle);
}`
答案 4 :(得分:1)
$email_exists = mysqli_query=("select email from $sql_table where email='$email'");
这个结果应该只是插入的电子邮件。
抱歉 修改强>
我认为应该是这样的:
$check=$email_exists->num_rows;
if($check==0){
//email doesn't exist
}
else{
//email does exist
}
如果有效,请告诉我。