how to avoid inserting duplicate records in mysql - Web Development and Web Design Codes

Latest

Wednesday, May 31, 2017

how to avoid inserting duplicate records in mysql

Prevent Duplicate Data Submission PHP MySQL

how to avoid inserting duplicate records in mysql
how to avoid inserting duplicate records in mysql
Hi Guys today we will Learn how to Get Deal with Duplicate Form Data Submission.
You Can Stop user To submit Duplicate Data.Such as 'Username,Email,Password etc...
Also Learn
1. PHP MySQL Pagination
2. PHP MySQL Login with SESSION
3. php mysql comment system
4. sort MySQL table column using PHP

5. PHP MySQL Shopping Cart

What you Will Need:

Database Creation:
Create Database Named: test
Table Named: user
Now use The Below SQL Codes to Create user Table.
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM CHARSET=latin1;

Files and Directory Creation:
Create 3 files named are Given Below.
1.connect.php
2.index.php
3.style.css
how to avoid inserting duplicate records in mysql
how to avoid inserting duplicate records in mysql

Now use The below codes for Differents Files.
connect.php
Create connect file to Connect our Server and Selecting Database.
<?php
$conn=new mysqli('localhost','root','','test');
?>

index.php
Create index.php file to show form and process and validate submitted user data.

<?php
//Creating Database Connection
include('connect.php');
if(isset($_POST['submit'])){
 //Getting Submited Data
 $username=$_POST['username'];
 $email=$_POST['email'];
 //Checking and Validating UserName
 if(!empty($username)){
   //Checking Database UserName Already Exist or Not
   $result=mysqli_query($conn,"SELECT*FROM user WHERE username='$username'");
   $count=$result->num_rows;
   if($count !==0){
    $error='';
    $nameerror='UserName Already Taken';
   }
 }else{
  $error='';
  $nameerror='Your UserName is Empty';
 }
 //Cheking for Email
 if(!empty($email)){
   //Checking Database Email Already Exist or Not
   $result=mysqli_query($conn,"SELECT*FROM user WHERE email='$email'");
   $count=$result->num_rows;
   if($count !==0){
    $emailerror='Email Already Taken';
    $error='';
   }
 }else{
  $error='';
  $emailerror='Your Email is Empty';
 }
 if(!isset($error)){
  //Inserting Data into user
  $result=mysqli_query($conn,"INSERT INTO user (username,email)VALUES('$username','$email')");
  if($result){
   $success='Your Information Successfully Saved';
  }else{
   $error='sorry failed..Try Agian';
  }
 }
}
?>


<html>
<head>
<title>PHP MySQL Duplicate Data Submission Prevent</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="form">
<h1>Prevent Duplicate data Submission Using PHP MySQL</h1>
<form method="post" action="">
<table>
   <tr>
      <td><label>UserName:*</label></td>
   <td><input type="text" name="username"/></td>
   <td><span><?php if(isset($nameerror)){echo $nameerror;}?></span></td>
   </tr>
   <tr>
      <td><label>Email:*</label></td>
   <td><input type="text" name="email"/></td>
   <td><span><?php if(isset($emailerror)){echo $emailerror;}?></span></td>
   </tr>
   <tr>
      <td></td>
   <td><input type="submit" name="submit" value="Submit"/></td>
   </tr>
</table>
</form>
<span><?php if(isset($success)){echo $success;}?></span>
<span><?php if(isset($error)){echo $error;}?></span>
</div>
</body>
</html>

style.css
Add Some CSS Codes to Design Index form Page.
#form{
 width:800px;
 height:250px;
 background:#ddd;
 color:green;
 margin:auto;
}
input[type=text]{
 margin-left:15px;
 padding:7px;
 width:250px;
 border-radius:5px
}
input[type=text]:focus{
 border:2px solid black;
 padding:7px;
}
input[type=submit]{
 margin-left:15px;
 padding:5px 8px;
 cursor:pointer;
 font-weight:bold;
 border-radius:5px;
 background:#080808;
 color:white;
}
label{
 font-size:20px;
 color:black;
 font-weight:bold;
 margin-left:20px;
}
span{
 font-size:20px;
 color:red;
}

Now You have Successfully Crated php mysql data validation system after submit.
Thank you for Visit.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.