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>
這似乎意義的,我製作的電子郵件地址只能註冊一次,但怎麼辦我執行這個? – Zippylicious
@Zippylicious。 。 。通過將約束添加到'members'表中,可以在數據庫中實現這一點。 –
我想通了。這很好,非常感謝。 – Zippylicious