user5443928
user5443928

Reputation:

How to prevent automatic data insert into DB while reload the page using PHP and MySQL

I have an issue.My previous data is inserting again and again at each time of page reload using PHP and MySQL.I am explaining my code below.

 $result = $dbobj->savePincode($newCustomerobj);
    if($result == 1)
    {
        $msg='3';
    }
    else
    {
        $msg='2';
    }
    function messagestatus(st)
{
    switch(st)
    {   
        case 1:
        {
            alert("Data Updated Successfully.");
            <?php header("location:localhost/demo/pincode.php"); ?>
            break;
        } 
        case 2:
        {
            alert("This user id already exist..");
            break;
        } 
        case 3:
        {
            alert("Data Saved Successfully");
            <?php header("location:localhost/demo/pincode.php"); ?>
            break;
        }
        case 4:
        {
            alert("Data deleted Successfully");
            <?php header("location:localhost/demo/pincode.php"); ?>
            break;
        }
    }
}
messagestatus(<?php echo $msg ?>);

Here i am also adding the header but still when user is reloading the page after first time insert again same is inserting.Please help me.

Upvotes: 0

Views: 804

Answers (1)

PHPExpert
PHPExpert

Reputation: 943

As far I get from your question, you are posting the form data using POST method and after that redirecting user to same page, so your data is getting insert again and again. Do small change as shown below in your code-

<?php header("location:localhost/demo/pincode.php?hash=".time()); ?>

By appending hash parameter with time() value, your browser will consider it as an new url and your form data will not get insert again.

Hope so it will work for your.

Upvotes: 1

Related Questions