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

Thanks,
Posted
Updated 26-May-22 2:05am
Comments
Uday P.Singh 22-Oct-11 3:08am    
can you post your code?
sreenathpktr 22-Oct-11 3:10am    
yes...here it is..
$k=$_POST['Return'];
$l=$_POST['Remarks'];
$btn=$_POST['submit'];

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

}
else
{
$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.

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


And You should create the demo.php file and paste this code:
PHP
<!--?php
    include 'conn.php';

    $name="";
    $date="";
    $destination="";
    $number="";
    $remarks="";
    $nights="";
    $days="";
    $price="";
    
    
    
    if(isset($_POST['submit'])){
        $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
?--></form>
 
Share this answer
 
v3

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