0

我正在练习 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();
    }
4

5 回答 5

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'];
于 2013-09-09T12:01:03.377 回答
3

在数据库中,将电子邮件 ID 列设为唯一。这应该解决它。当有人尝试使用相同的 id 注册时,创建相应的错误消息。

于 2013-09-09T12:28:33.557 回答
2

header('Location: index.php');语句在echo "Email is already registered.";处理之前重定向您的页面。

因此,为了看到显示的错误消息重定向到其他页面 Like header('Location: error.php');whereerror.php显示错误消息或删除语header('Location: index.php');​​句

于 2013-09-09T12:14:37.897 回答
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
}

请让我知道它是否有效。

于 2013-09-09T14:07:31.887 回答
1

回显后删除 header() 函数,您的消息将可见。

`if ($db_found && mysqli_num_rows($email_exists) > 0) {

echo "Email is already registered.";
mysqli_close($db_handle);

}`

于 2013-09-09T12:25:17.327 回答