All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Prevent Multiple And Duplicate Form Submission Using PHP And MySQL

Last Updated : Jul 1, 2023

IN - PHP MySQL | Written & Updated By - Anjali

In this tutorial we will show you how to prevent multiple and duplicate form submission using PHP and 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.

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.

I hope this tutorial on prevent multiple and duplicate form submission using PHP and MySQL helps you and the steps and method mentioned above are easy to follow and implement.