I am studying PHP with a book by Luke Wellin and Laura Thomson and I am in the part of storage and retrieval of files. I am creating a text file that stores the data received from a form orderform.html
<!DOCTYPE html>
<html>
<head>
<title>Pedidos de Bob</title>
<meta charset="utf-8">
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Form</h2>
<form action="processorder.php" method="POST">
<table>
<tr style="background-color: silver">
<td style="width: 150px">Items</td><td style="width: 260px">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td><input type="text" name="tireqty" size="3"></td>
</tr>
<tr>
<td>Oil</td>
<td><input type="text" name="oilqty" size="3"></td>
</tr>
<tr>
<td>Spark Plus</td>
<td><input type="text" name="sparkqty" size="3"></td>
</tr>
<tr>
<td>Shipping Address</td>
<td><input type="text" name="address" style="width: 250px"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit Order"></td>
</tr>
</table>
</form>
</body>
</html>
And this is the code processorder.php
<?php
//Creación de variables cortas.
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date('h:i, jS F Y');
?>
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order Processed at ".$date."</p>";
echo "<p>Your order is as follows</p>";
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo "Items Ordered ".$totalqty."<br />";
if ($totalqty == 0) {
echo "You did not order anything on the previous page!";
} else {
if ($tireqty > 0) {
echo $tireqty." Tires<br />";
}
if ($oilqty > 0) {
echo $oilqty." Botles of Oil<br />";
}
if ($sparkqty > 0) {
echo $sparkqty." Spark Plugs<br />";
}
}
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 100);
define('SPARKPRICE', 100);
$totalamount = TIREPRICE*$tireqty + OILPRICE*$oilqty + SPARKPRICE*$sparkqty;
$totalamount = number_format($totalamount, 2, '.',' ');
echo "<p>Total of order is: $".$totalamount."</p>";
echo "<p>Address to shop to is: ".$address."</p>";
//Creamos la variable outputstring para capturar el pedido y pasarlo a un archivo
$outputstring = $date."\t".$tireqty." Tires \t".$oilqty." Oil\t" .$sparkqty." Spark Plugs\t\$".$totalamount."\t". $address."\n" ;
//Abrimos el archivo para anexar, no es necesario que archivo orders.txt exista ya que
//con 'a' adjuntamos (escribimos) y si no existe el archivo intenta crearlo.
// $DOCUMENT_ROOT toma como valor C:/Apache24/htdocs y luego regresamos un nivel en el rirectorio con ../
// Finalmente para ingresar a la carpeta orders (esta carpeta ya debe existir) donde se creará el archivo orders.txt
$fp = @fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');
//"bloqueamos" el archivo antes de leer o escribir en el
flock($fp, LOCK_EX);
//Si $fp no toma valor verdadero en otras parablas (!$fp)=(False)
if (!$fp) {
echo "<p><strong>Your order could not be processed at this time. Please try again later</strong></p>";
exit;
}
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
echo "<p>Order Writen</p>";
?>
</body>
</html>
After pressing the send button, the data is correctly registered.
And it is also displayed correctly in the text file orders.txt
The problem is that being on the web where it shows the successful result when adding a record, I update the page and insert another record in the file orders.txt and I would like you to help me avoid it happening since if the user accidentally updates the page, he would also be creating duplicate records the times he has clicked on updating the page.