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

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 coding is this coding is for register.

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

    I would consider using a php redirect to keep it from happening. If you look at the changes in your code below, you'll see that there's two additions. First, we redirect the page to itself with a GET var of "success". Then we look for that var so we can tell them that the form submission succeeded.

    This way if the page is refreshed, it will refresh the redirect, which has no post data associated with it.

    <!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')");
    
    header('Location: '.$_SERVER['PHP_SELF'].'?success');
    exit;
    
    }
    
    if(ISSET($_GET['success'])){
    
    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> 

      thanks for your advice schwim but i am confuse because you use success here

      if(ISSET($_GET['success'])){

      but ihave used name as a submit button name so is it right to use success instead of submit ??

        means i used submit as a button name but you use success so is it right ???

          i use your code but now it is gining me this error

          Cannot modify header information - headers already sent by (output started at D:\vicky host files important\htdocs\toolshop\register.php:46) in D:\vicky host files important\htdocs\toolshop\register.php on line 120

            schwim's suggestion is essentially correct. The problem is that you are processing the form midway in the page, by which time output has been sent to the client. When this happens, you can no longer make a header() call unless you use output buffering.

            What you should do rather is to move your form processing to the very start, before even a single byte of output has been sent to the client. Check that the form has been POSTed, and if so process it. If it is invalid, set variables so that you can later display the errors to the user. If it is valid, do the insertion to the database then redirect the user (possibly to the same page).

              Laserlight caught my mistake already so all you need to do is move:

              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')");
              
              header('Location: '.$_SERVER['PHP_SELF'].'?success');
              exit;
              
              } 

              Which will allow the redirect since no HTML will have yet been passed to the browser.

                Incidentally, this is known in the business as 'redirect after post' (or, alternatively, PRG [for POST/REDIRECT/GET])and is de rigeur for things like registration & search form handling, although with registration you generally check to see if the data is already present and could then just show an error page.

                  laserlight;11049957 wrote:

                  schwim's suggestion is essentially correct. The problem is that you are processing the form midway in the page, by which time output has been sent to the client. When this happens, you can no longer make a header() call unless you use output buffering.

                  What you should do rather is to move your form processing to the very start, before even a single byte of output has been sent to the client. Check that the form has been POSTed, and if so process it. If it is invalid, set variables so that you can later display the errors to the user. If it is valid, do the insertion to the database then redirect the user (possibly to the same page).

                  thanks for your suggestion i will try this tonight.

                    vickymehraseo;11049967 wrote:

                    thanks for your suggestion i will try this tonight.

                    vickymehraseo;11049969 wrote:

                    thanks for your suggestion i will try this tonight.

                    Hehe ... did you refresh THIS page, by any chance? :p

                      Write a Reply...