Preventing refresh from duplicating db insert.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Preventing refresh from duplicating db insert.

Post 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?
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

No, because it needs to continue and display the page.
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post 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..)
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post 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;
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.)
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

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