Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Prevent Multiple And Duplicate Form Submission Using PHP And MySQL

Friday, Dec 15, 2017

Tags:- PHP MySQL


After submitting the form there are some situation when user reload the page or click on submit button again just to ensure that his data is submitted due to this we get many rows of duplicate data in our database. So ,in this tutorial we will show you how to prevent multiple and duplicate form submission using PHP and MySQL.You may also like validate email and password using jQuery.


Prevent Multiple And Duplicate Form Submission Using PHP And MySQL


To Prevent Multiple And Duplicate Form Submission It Takes Only Two Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to check and store data in database


Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name form.html

<html>
<body>
<div id="wrapper">

<div id="form_div">
 <form action="store_detail.php" method="post">
  <input type="text" name="name" placeholder="Enter Name"/>
  <input type="text" name="email" placeholder="Enter Email"/>
  <input type="password" name="password" placeholder="********"/>
  <input type="submit" name="submit_data" value="SUBMIT"/>
 </form>
</div>

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

In this step we create a form to send user details to 'store_detail.php' which store user data after checking of duplicate data.You may also like jQuery form validation.



Step 2.Make a PHP file to check and store data in database

We make a PHP file and save it with a name store_detail.php

// Database Structure 
CREATE TABLE 'user' (
 'name' text NOT NULL,
 'email' text NOT NULL,
 'password' text NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php
if(isset($_POST['submit_data']))
{
 $host="localhost";
 $username="root";
 $password="";
 $databasename="sample";
 $connect=mysql_connect($host,$username,$password);
 $db=mysql_select_db($databasename);	 

 $name=$_POST['name'];
 $email=$_POST['email'];
 $pass=$_POST['password'];
	
 $get_user=mysql_query("select * from user where name='$name',email='$email' and password='$pass'");
 if(mysql_num_rows($get_user)>0)
 {
  echo "Details Are Already Submitted";
 }
 else
 {
  mysql_query("insert into user values('$name','$email','$pass')");
  header("location:Your Webpage");
 }
}
?>

In this step we create a database called 'user' to store user details on signup.We get the values of name, email and password and check if the user with these details is present or not if user is present we display 'details are already submitted' to prevent storing of duplicate data in database and if details are not present we insert the details in database and use header() function to redirect user to current page to prevent multiple form submission.


That's all, this is how to prevent multiple and duplicate form submission using PHP and MySQL.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me


Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.