1

我一直在制作登录/注册系统,我遇到的一个问题是不允许注册重复的电子邮件地址。我希望它能够工作,以便数据库不会接受来自重复电子邮件的数据,并且用户也会收到警报。我对 PHP 有点陌生,所以我不确定如何做到这一点。谢谢。

我的 PHP

if (empty($_POST['email'])) {
    $error[] = 'Please Enter your Email ';
} else {

    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-   ]+)+$/", $_POST['email'])) {
       //regular expression for email validation
        $Email = $_POST['email'];
    } else {
         $error[] = 'Your Email Address is invalid  ';
    }


}


if (empty($_POST['Password'])) {
    $error[] = 'Please Enter Your Password ';
} else {
    $Password = $_POST['Password'];
}


if (empty($error)) //send to Database if there's no error '

{ // If everything's OK...

    // Make sure the email address is available:
    $query_verify_email = "SELECT * FROM members  WHERE Email ='$Email'";
    $result_verify_email = mysqli_query($dbc, $query_verify_email);
    if (!$result_verify_email) {//if the Query Failed ,similar to if($result_verify_email==false)
        echo ' Database Error Occured ';
    }

    if (mysqli_num_rows($result_verify_email) == 0) { // IF no previous user is using this email .


        // Create a unique  activation code:
        $activation = md5(uniqid(rand(), true));


        $query_insert_user = "INSERT INTO `members` ( `Username`, `Email`, `Password`, `Activation`) VALUES ( '$username', '$Email', '$Password', '$activation')";


        $result_insert_user = mysqli_query($dbc, $query_insert_user);
        if (!$result_insert_user) {
            echo 'Query Failed ';
        }

        if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.


mysqli_close($dbc);//Close the DB Connection

 } // End of the main Submit conditional.

?>

的HTML

<form action="./index.php#openModal2" method="post" class="registration_form">
  <fieldset>
<legend>Registration Form </legend>

<p>Create A new Account</p>

<div class="elements">
  <label for="username">Name :</label>
  <input type="text" id="username" name="username" size="25" />
</div>
<div class="elements">
  <label for="email">E-mail :</label>
  <input type="text" id="email" name="email" size="25" />
</div>
<div class="elements">
  <label for="Password">Password:</label>
  <input type="password" id="Password" name="Password" size="25" />
</div>
<div class="submit">
 <input type="hidden" name="formsubmitted" value="TRUE" />
  <input type="submit" value="Register" />
</div>

4

2 回答 2

5

email在表中的列上添加唯一约束members

ALTER TABLE members ADD UNIQUE (email);

通常,您会在创建表时执行此操作,而不是事后更改表。

于 2013-07-21T14:17:30.380 回答
-1

要么像 Gordon Linoff 所说的那样添加一个独特的约束,要么这就是我所做的......

$check_email_for_duplicates = mysqli_query($dbc, "select * from `members` where `Email` = '".mysqli_real_escape_string($email)."'");

    if(mysqli_num_rows($check_email_for_duplicates) > 0) //Email address is unique within this system and must not be more than one
    {
        echo 'Sorry, the  email <b>'.$email.'</b> is already in use. Please enter a different email.';
    }
    else {
     //some code
 }
于 2013-07-21T14:24:02.937 回答