Preventing refresh from duplicating db insert.
Posted: Sun Feb 08, 2004 7:27 pm
I've seen this in the forum before, but I don't recall the solution and now I cannot find it. Consider this:
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?
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
}
?>