Page 1 of 1

how stop insert data in the datbase when refreshing php page

Posted: Sun Aug 09, 2015 3:55 am
by vickymehraseo
sir when i am inserting values from form to database table it is fine but when i click on refresh or refresh with F5 button than values are again inserting in the table and i should tell you i also used this code :-

***** PLEASE USE THE PHP CODE TAG *****

Code: Select all

if (isset($_POST['submit'])){


}

so tell me how can i solve this problem. and i how can i prevent
duplicate record insertion while refresh the php page?

my complete coding is.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Tools Shop</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<link rel="stylesheet" type="text/css" href="style.css" />
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="iecss.css" />
<![endif]-->
<script type="text/javascript" src="js/boxOver.js"></script>
</head>
<body>

<div id="main_container">
<?php 

include "includes/header.php";
?>

<?php 

include "includes/left.php";
?>



    <div class="center_content">



<form action="" method="post">

Name<span style="color:red;">*</span> <input type="text" name="name" placeholder="Name" style="margin-left:39px;" /><br /><br />
Email<span style="color:red;">*</span> <input type="text" name="email" placeholder="Email" style="margin-left:42px;" /><br /><br />
Password<span style="color:red;">*</span> <input type="text" name="password" placeholder="Password" /><br /><br />
Contact<span style="color:red;">*</span> <input type="text" name="contact" placeholder="Contact No." style="margin-left:30px;" /><br /><br />
Address<span style="color:red;">*</span> <input type="text" name="address" placeholder="Address" style="margin-left:28px;" /><br /><br />
Gender :
<input type="radio" checked="checked" name="gender" value="Male" />Male
<input type="radio" checked="checked" name="gender" value="Female" />Female<br /><br />

<input type="submit" value="submit" name="submit" style="margin-left:70px;"/>

</form>

<?php 

if (isset($_POST['submit'])){

$name= $_POST['name'];
$email= $_POST['email'];
$password= $_POST['password'];
$contact= $_POST['contact'];
$address= $_POST['address'];
$gender=$_POST['gender'];

		$mysqli->query("INSERT INTO members (name,email,password,contact,address,gender) 
		VALUES('$name','$email','$password','$contact','$address','$gender')");
		
		echo "<h2>Successfully Registered</h2>";
}


?>




    </div>
    <!-- end of center content -->

<?php 

include "includes/right.php";
?>

	
    <!-- end of right content -->
  </div>
  <!-- end of main content -->
<?php 

include "includes/footer.php";
?>
  
<!-- end of main_container -->
</body>
</html>

Re: how stop insert data in the datbase when refreshing php

Posted: Sun Aug 09, 2015 8:36 am
by yacahuma
Thats how web pages work. If you press F5 it will re-post that page. If you dont want the inserts duplicated, then you may need to create unique keys in your database. Another way will be to store the time you submit and if the user tries to submit again in less than 10 seconds, dont allow that.

At the end you are trying to fight the standard web behavior, which I dont thing is a good idea.

Re: how stop insert data in the datbase when refreshing php

Posted: Sun Aug 09, 2015 8:43 pm
by Christopher
The method used to prevent web form from being reposted when the page is refreshed is to have the script redirect to a success page using the header() tag. Instead of displaying "Successfully Registered", redirect to a different page that displays that message. Typically with MVC Controllers that is a different Action in the Controller.