Page 1 of 1

Preventing refresh from duplicating db insert.

Posted: Sun Feb 08, 2004 7:27 pm
by Bill H
I've seen this in the forum before, but I don't recall the solution and now I cannot find it. Consider this:

Code: Select all

<?php
if (isset($_POST['Addrec']))        // the addition function above was executed to add an item
{
     $Tvar = "I";                                                          // assume an income item
     if (isset($_POST['Cex_x']) || isset($_POST['Lex_x'])) $Tvar = "E";    // adding an expense item
     if (isset($_POST['Cmi_x']) || isset($_POST['Lmi_x'])) $Tvar = "M";    // adding an mileage item
     $Cvar = "N";
     if (isset($_POST['Cin_x']) || isset($_POST['Cex_x']) || isset($_POST['Cmi_x'])) $Cvar = "Y";

     $Query = "INSERT INTO Exprec (Tref,Type,Calc) VALUES (" . $_POST['Addrec'] . ",'$Tvar','$Cvar')";
     mysql_query($Query, $Link);      // add the detail and re-display the time card
     $Vrec = $_POST['Addrec'];        // view the item with the added record
     unset($_POST);                   // to allow refresh
}
if ($Vrec)        // selected an individual timecard for editing
{
     // code here to display the records
}
?>
For a number of reasons, a "refresh" of the page is very common, and when that happens a duplicate record is inserted into the database. Unsetting the $_POST array should solve that, but it doesn't. How can I allow a refresh of the page without a duplicate database record being created?

Posted: Sun Feb 08, 2004 7:47 pm
by Michael 01
What I am seeing is this: unset($_POST);

Rite behind it, the explanation says "allows for refresh".

Have you tried a exit statement rite after that? (i noticed you had some code that needs to go after this call, so perhaps that wont work, but it will at least stop the refresh query.

Posted: Sun Feb 08, 2004 8:16 pm
by Bill H
No, because it needs to continue and display the page.

Posted: Sun Feb 08, 2004 8:36 pm
by Michael 01
Yea, thats what I thought...What is the query: $link all about. Is that the URL, or is that something for the DB call?

If its a URL thing, it could be literally refreshing because of that...(like clicking on the link...etc..)

Posted: Sun Feb 08, 2004 8:41 pm
by Michael 01
The other thing is this:$Vrec = $_POST['Addrec'];

in the next few lines of code, towards the end..

Code: Select all

if ($Vrec)


So, in short, its posting, and than the next line (if vrec) is going back and saying "yes, it posted"...this creates the refresh i guess.....just a different way of refreshing data i guess. A person could just have another .php scipt/page that shows the results, and eliminate that all together..

Code: Select all

if ($Vrec)        // selected an individual timecard for editing 
&#123; 
  header(results.php);
&#125;

Posted: Mon Feb 09, 2004 8:26 am
by Bill H
What is the query: $link all about.
if you mean
$Result = mysql_query($Query, $Link);
That's a function call that executes a MySQL database query.

I put the code that adds the new detail into a seperate script and used a header to return to this script (which contains revision code as well as display code.)

Posted: Mon Feb 09, 2004 6:49 pm
by Michael 01
Ok...that is what I kind of thought..but you never know.

I guess I am stumped than. All of the typical reasons are not viable as to why it chooses to loop through or basically refresh. :x

Posted: Mon Feb 09, 2004 7:08 pm
by d3ad1ysp0rk

Code: Select all

<?php
session_start();
$var = $_SESSION['var'];
if($var!="1"){
//process
$_SESSION['var']=="1";
}
else {
//do not process
}
?>