php - How to prevent duplicate email addresses from being registeres? -
i have been making login/register system , 1 problem have run not allowing duplicate email addresses being registered. want work database wont accept data duplicate email , user alerted too. sort of new php unsure of how this. thanks.
my php
if (empty($_post['email'])) { $error[] = 'please enter 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 email validation $email = $_post['email']; } else { $error[] = 'your email address invalid '; } } if (empty($_post['password'])) { $error[] = 'please enter password '; } else { $password = $_post['password']; } if (empty($error)) //send database if there's no error ' { // if everything's ok... // make sure email address available: $query_verify_email = "select * members email ='$email'"; $result_verify_email = mysqli_query($dbc, $query_verify_email); if (!$result_verify_email) {//if query failed ,similar if($result_verify_email==false) echo ' database error occured '; } if (mysqli_num_rows($result_verify_email) == 0) { // if no previous user using email . // create unique activation code: $activation = md5(uniqid(rand(), true)); $query_insert_user = "insert `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 insert query successfull. mysqli_close($dbc);//close db connection } // end of main submit conditional. ?>
the html
<form action="./index.php#openmodal2" method="post" class="registration_form"> <fieldset> <legend>registration form </legend> <p>create 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>
add unique constraint on email
column in table members
:
alter table members add unique (email);
typically, when create table rather altering table afterwards.
Comments
Post a Comment