tags:

views:

96

answers:

3

i am writing code to insert data into mysql on same page from where i retrive data by post method . it working fine but when i refresh page it will insert again. my question is that how to stop reexcuting the insert query on refreshing page

+1  A: 

This is being caused by the POST data being sent again when you refresh. Your browser should give you a warning that it's doing that. If you're not getting that warning, you might be using GET instead of POST which is not a good idea for actions which change or insert data.

Check your form has this attribute:

<form method="post">

If you want to avoid it altogether, just redirect the browser after the post back.

<?php
if ($_POST) {
    // insert into database
    header("location: thisPage.php");
    // don't bother with die() here
}

Since the redirect is not being used for security purposes, and you're just redirecting back to the same page, it's not really necessary to die() afterwards.

nickf
A: 

Add some variable to your form and then during saving reset it's value.

if(isset($_POST['save']))
{ //do your saving
  $save="";    
}
Riho
A: 

You probably want to use the Post-Redirect-Get pattern to resolve this situation. It's good that you use POST to send data to your server. After you've done the necessary modifications, redirect your user to the page you want him to see, with a simple redirect-header. This will NOT break the back button, and a click on it will take the user back to the form, without sending the data again.

PatrikAkerstrand