how to avoid inserting duplicate records in mysql - Web Development and Online Earning

Breaking

Web Development and Online Earning

Learn Web Development ,PHP MySQL,Codeigniter,AJAX,HTML CSS,Javascript,Jquery,Web Hosting,Online Earning,SEO,Link Building,Online Banking and More

test banner

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

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...

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 AUTO_INCREMENT=5 DEFAULT 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
$host="localhost"; //Host Name
$username="root"; //MySQL Username
$password=""; //MySQL Password
$dbname="test"; //Database Name
//Connect To The Server
mysql_connect("$host","$username","$password");
//select database
mysql_select_db("$dbname");
?>

index.php
Create index.php file to show form and process submited data.
Add the Below PHP Codes top of The index.php page to Validate and Process the Input Details.
<?php
include("connect.php");
if(isset($_POST['submit'])){
 //Empty Fields Validation
 if(empty($_POST['username'])||empty($_POST['email'])){
  $mandatory= "* Marked Fields are Mandatory.Can not Be Empty";
 }else{
 //Getting POTED username and Email Value
 $name=$_POST['username'];
 $email=$_POST['email'];
//Checking Database for Duplicate Value for username
$namecheck="SELECT*FROM user WHERE username='$name'";
$nameresult=mysql_query($namecheck);
$namecount=mysql_num_rows($nameresult);
if($namecount==0){
 //Checking Database for Duplicate Value for Email
$emailcheck="SELECT*FROM user WHERE email='$email'";
$emailresult=mysql_query($emailcheck);
$emailcount=mysql_num_rows($emailresult);
if($emailcount==0){
 //Inserting Fresh Data Into Database
  $sql="INSERT INTO user (username,email)VALUES('$name','$email')";
  $result=mysql_query($sql);
  if($result){
   $success= "<font color=green><strong>Your Information Successfully Saved</strong></font>";
  }else{
   $error= "Sorry We are Unable To Save your Information";
  }
}else{
 $emailerror="Email <font color=green><i><strong>'$email'</strong></i></font> Already Exist.Please Try a New One";
}
}else{
 $nameerror= "UserName <font color=green><i><strong>'$name'</strong></font> Already Exist.Please Try a New One||";
}
}
}
?>

Now Add the Below HTML codes after PHP Codes to Display Input Form.
<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="">
<label>UserName:*</label><br/>
<input type="text" name="username"/><br/>
<label>Email:*</label><br/>
<input type="text" name="email"/><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<span><?php if(isset($success)){echo $success;}?></span>
<span><?php if(isset($nameerror)){echo $nameerror;}?></span>
<span><?php if(isset($emailerror)){echo $emailerror;}?></span>
<span><?php if(isset($mandatory)){echo $mandatory;}?></span>

</div>
</body>
</html>

style.css
Add Some CSS Codes to Design Index form Page.
#form{
 width:800px;
 height:250px;
 background:#ddd;
 color:green;
}
input[type=text]{
 padding:7px;
 width:30%;
 border-radius:5px
}
input[type=text]:focus{
 border:2px solid black;
 padding:8px;
}
input[type=submit]{
 padding:5px;
 border-radius:5px;
 background:#080808;
 color:white;
}
label{
 font-size:20px;
 color:black;
 font-weight:bold;
}
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

Post Top Ad

Responsive Ads Here