Bambang Rahmadi K.P Bambang Rahmadi K.P - 3 months ago 15
MySQL Question

how to prevent duplicate username and email data in php/mysql

how to prevent duplicate username and email data.



I have been making a login/register system and I am drawing close to finishing my register portion of code. The only problem I am running into is how to make it so that users cannot register with duplicated usernames and e-mail. I want it to work so that my database wont accept the information, and it will tell the user about the error. Any help is appreciated.

This is my source code :



=================================================================

<!-- daftar -->
<section id="daftar" style="background: #333;">
<div class="container">
<div class="row text-center">
<div class="col-lg-10 col-lg-offset-1">
<h2 style="font-family:Font Juice; font-size:50px; color:#ccc; padding: 20px;">Daftar Baru</h2>
<hr class="small">
<div class="row">
<div class="col-lg-8 col-lg-offset-2" >
<form name="contactForm" method="post" id="contactForm" action="<?php $_SERVER['PHP_SELF'];?>">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<input type="text" class="form-control" placeholder="Nama Anda" name="t1" id="t1" required data-validation-required-message="Input nama anda.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<input type="email" class="form-control" placeholder="Email" name="t2" id="t2" required data-validation-required-message="Input alamat email.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<input type="text" class="form-control" placeholder="Username" name="t3" id="t3" required data-validation-required-message="Input username untuk login">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<input type="password" class="form-control" placeholder="Password" name="t4" id="t4" required data-validation-required-message="Input password untuk login">
<p class="help-block text-danger"></p>
</div>
</div>

<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<input type="password" class="form-control" placeholder="Konfirmasi Password" name="t5" id="t5" required data-validation-required-message="Konfirmasi Password">
<p class="help-block text-danger"></p>
</div>
</div>

<script type="text/javascript">
window.onload = function () {
document.getElementById("t4").onchange = validatePassword;
document.getElementById("t5").onchange = validatePassword;
}
function validatePassword(){
var t4=document.getElementById("t4").value;
var t5=document.getElementById("t5").value;
if(t4!=t5)
document.getElementById("t5").setCustomValidity("Passwords tidak Sama, Coba Lagi");
else
document.getElementById("t5").setCustomValidity('');
}
</script>

<div class="row">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label class="checkbox-inline">
<h5 style="color:#fff; font-family: BebasNeueRegular; font-size: 24px">Dengan menekan tombol " Daftar "<br> Saya Setuju dengan <a href="#">Kebijakan dan Ketentuan</a> yang berlaku.</h5>
</label>
</div>

<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-primary btn-lg" style="font-family:'BebasNeueRegular'; font-size:20px; text-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);" name="btnSimpan" id="btnSimpan"> <span class="glyphicon glyphicon-list"></span> &nbsp;&nbsp;Daftar&nbsp;&nbsp;</button>
<br><br><br>
</div>
</div>
<div id="success"></div>
</form>

<?php
if(isset($_POST['btnSimpan'])){
$q = $op->save("tb_user","nama_user,email_user,username,password","'".$_POST['t1']."','".$_POST['t2']."','".$_POST['t3']."','".md5($_POST['t4'])."'");
if($q){
echo "<div class='alert alert-success'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
<strong>Success!</strong> Data berhasil disimpan. Silahkan klik tombol Mulai Bermain.</div>";
}else{
echo "<div class='alert alert-danger' >
<a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
<strong>Error !</strong> Data gagal disimpan</div>";
}
}
?>
</div>
</div>



Answer

Create a UNIQUE CONSTRAINT on email_user and username column in your tb_user table; which will throw error if you try to insert duplicates.

ALTER TABLE tb_user ADD CONSTRAINT constr1 UNIQUE (email_user);