Click here to Skip to main content
15,606,568 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
When i refresh the page the datas are inserting to database.How can i avoid this?

Updated 26-May-22 2:05am
Uday P.Singh 22-Oct-11 3:08am    
can you post your code?
sreenathpktr 22-Oct-11 3:10am it is..

$ins=mysql_query("insert into it_clearance2 values('$k','$l')");
$msg="IT clearance record saved successfully";
header("Refresh:3; url=it.php");

$msg="Error occured..";

the HTTP protocol is stateless and no matter what you do, $_POST will always contain the same data coz thats what browsers do, they save the POST data and which is why the $btn is never unset or emptied and which in turn is why it keeps inserting into the table.

Having said that, what you can probably try doing is, don't refresh the page and as soon as the values are inserted the first time and you have a result $msg, redirect to a different page to show that message. You can append that message in querystring of the redirected to URL or you can put it in a session and retrieve it on the other page.

Hope this helps...

EDIT: move the call to header() function outside the if else if you want the failure message to be conveyed to the redirected to page.

Share this answer
First you should redirect the form into another page like:
<form action="demo.php" method="POST">

And You should create the demo.php file and paste this code:
    include 'conn.php';

        $name = $_POST['name'];
        $date = date('Y-m-d', strtotime($_POST['date']));
        $destination = $_POST['destination'];
        $number = $_POST['number'];
        $remarks = $_POST['remarks'];
        $nights = $_POST['nights'];
        $days = $_POST['days'];
        $price = $_POST['price'];
    $query = "INSERT INTO date (name,date,destination,number,remarks,nights,days,price) VALUES ('$name','$date','$destination','$number','$remarks','$nights','$days','$price')";
    $iquery = mysqli_query($con, $query);

    header("location:testing.php"); //Redirecting To the main page
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900