php - 如何防止注册重复的电子邮件地址?

标签 php mysql duplicates

我一直在制作一个登录/注册系统,我遇到的一个问题是不允许注册重复的电子邮件地址。我希望它能正常工作,这样数据库就不会接受来自重复电子邮件的数据,并且用户也会收到警报。我是 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>

最佳答案

在表 membersemail 列上添加唯一约束:

ALTER TABLE members ADD UNIQUE (email);

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

关于php - 如何防止注册重复的电子邮件地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17773250/

相关文章:

php - 共享主机有哪些第三方搜索引擎软件/库

mysql - 将 JSON 文件本身存储在 MYSQL DB 中

mysql - 添加外键约束

MySQL : Updating a table having a 'where' clause with a max value

Java - 如何检查字符串中的重复字符?

javascript - 删除数组中的重复对象,保留具有最大属性值的对象

php - 使用多个连接连接查询字符串

php - 连接多个用户条目并提交到单个 MySQL 数据库条目

.net - SQL Server 中排除具有不同 ID(自动增量)的重复行的 SELECT 语句

php - Laravel中的前端错误显示架构