lional Posted September 25, 2008 Share Posted September 25, 2008 I have a problem. If the user presses the refresh key on their browser another entry gets added to the db table How can I stop this. I have tried the following: $query = 'SELECT * FROM products WHERE prod_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query_ex_user = "SELECT * from line_items WHERE ref_no = '{$_SESSION['ref']}' AND prod_code = '$key' AND filename = '$filename_out'"; $result_ex_user = mysql_query($query_ex_user); $num_ex_user = @mysql_num_rows($result_ex_user); if ($num_ex_user == 0) { // Insert the line items $query_li = "INSERT INTO line_items (ref_no, prod_code, qty, filename, edge, effect, date) VALUES ('{$_SESSION['ref']}', '$key', '$value', '{$_SESSION['line_file']}', '$edging_out', '$effects_out', CURDATE())"; $result_li = @mysql_query ($query_li); } $query .= $key . ','; } $query = substr ($query, 0, -1) . ') ORDER BY size_mm ASC'; $result = mysql_query ($query); Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/ Share on other sites More sharing options...
Maq Posted September 25, 2008 Share Posted September 25, 2008 Out of curiosity why don't you have this submitted when the user clicks a submit button? and not just having the page run? Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/#findComment-650214 Share on other sites More sharing options...
lional Posted September 25, 2008 Author Share Posted September 25, 2008 I am still a php newbie so I am still learning as I go along, please enlighten me Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/#findComment-650222 Share on other sites More sharing options...
Maq Posted September 25, 2008 Share Posted September 25, 2008 Post the whole script please. Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/#findComment-650234 Share on other sites More sharing options...
Psycho Posted September 25, 2008 Share Posted September 25, 2008 When the page is submitted tot he processing page, have the processing page insert the record and then immediately redirect to a confirmation page. If the user hits refresh he/she will just see the confirmation page. Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/#findComment-650235 Share on other sites More sharing options...
lional Posted September 25, 2008 Author Share Posted September 25, 2008 <?php session_start(); $prodid_out = $_SESSION['prodid']; $reference_out = $_SESSION['ref']; $edging_out = $_SESSION['edge']; $effects_out = $_SESSION['effects']; $filename_out = $_SESSION['filename']; ?> <body bgcolor="#32818B" topmargin="0"> <?php include 'top.php'; ?> <table width="790" align="center" bgcolor="#b3314b" cellpadding="0" cellspacing="0"> <tr><td height="450"> <?php include ('includes/conn_db.php'); // Connect to the database. // Retrieve all of the information for the prints in the cart. $query = 'SELECT * FROM products WHERE prod_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query_ex_user = "SELECT * from line_items WHERE ref_no = '{$_SESSION['ref']}' AND prod_code = '$key' AND filename = '$filename_out'"; $result_ex_user = mysql_query($query_ex_user); $num_ex_user = @mysql_num_rows($result_ex_user); if ($num_ex_user == 0) { // Insert the line items $query_li = "INSERT INTO line_items (ref_no, prod_code, qty, filename, edge, effect, date) VALUES ('{$_SESSION['ref']}', '$key', '$value', '{$_SESSION['line_file']}', '$edging_out', '$effects_out', CURDATE())"; $result_li = @mysql_query ($query_li); } $query .= $key . ','; } $query = substr ($query, 0, -1) . ') ORDER BY size_mm ASC'; $result = mysql_query ($query); // Create a table and a form. print <<<HEAD <table summary="" width="700" align="center" bgcolor="#b3314b"> <tr> <td nowrap align="center"><font face="arial" size="2" color="white"><b>YOUR SHOPPING CART</b></font></tr></table> HEAD; echo '<table summary="" align="center" width="570" border="1" > <tr> <td> <table border="0" width="700"> <tr> <td align="left" width="110"><font face="arial" size="2" color="white"><b>Photo</b></font></td> <td align="left" width="100"><font face="arial" size="2" color="white"><b>Product</b></font></td> <td align="left" width="100"><font face="arial" size="2" color="white"><b>Size in mm</b></font></td> <td align="left" width="100"><font face="arial" size="2" color="white"><b>Edging</b></font></td> <td align="left" width="100"><font face="arial" size="2" color="white"><b>Finish</b></font></td> <td align="center" width="10"><font face="arial" size="2" color="white"><b>Qty</b></font></td> <td align="right" width="70"><font face="arial" size="2" color="white"><b>Unit Price</b></font></td> <td align="right" width="70"><font face="arial" size="2" color="white"><b>Line Total</b></font></td> </tr>'; // Print each item. $total = 0; // Total cost of the order. /* if ($row['specials_discount'] > 0) { $list_price = $row['price'] / $row['specials_discount']; } else { $list_price = $row['price']; } $list_price = $row['price']; $list_price = number_format($list_price, 2, '.', ''); // Calculate the total and sub-totals. $subtotal = $_SESSION['cart'][$row['prod_id']] * $list_price; $total += $subtotal; $subtotal = number_format($subtotal, 2, '.', ''); $total = number_format($total, 2, '.', ''); if ($_SESSION['cart'][$row['prod_id']] > 0) {*/ // Print the row. $query_cl = "SELECT * from line_items WHERE ref_no = '$reference_out' AND qty > '0'"; $result_cl = mysql_query($query_cl, $conn); while ($row_cl = mysql_fetch_assoc($result_cl)){ $prod_code_out = $row_cl["prod_code"]; $qty_out = $row_cl["qty"]; $filename_out = $row_cl["filename"]; $edge_out = $row_cl["edge"]; $effect_out = $row_cl["effect"]; $query_prod = "SELECT * from products WHERE prod_id = '$prod_code_out'"; $result_prod = mysql_query($query_prod, $conn); while ($row_prod = mysql_fetch_assoc($result_prod)){ $description_out = $row_prod["description"]; $size_mm_out = $row_prod["size_mm"]; $price_out = $row_prod["price"]; $subtotal = $price_out * $qty_out; $subtotal = number_format($subtotal, 2, '.', ''); $total = $total + $subtotal; $total = number_format($total, 2, '.', ''); print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="white">$filename_out</font></td> <td align="left"><font face="arial" size="2" color="white">$description_out</font></td> <td align="left"><font face="arial" size="2" color="white">$size_mm_out</font></td> <td align="left"><font face="arial" size="2" color="white">$edge_out</font></td> <td align="left"><font face="arial" size="2" color="white">$effect_out</font></td> <td align="center"><font face="arial" size="2" color="white">$qty_out </td> <td align="right"><font face="arial" size="2" color="white">R $price_out</font></td> <td align="right"><font face="arial" size="2" color="white">R $subtotal</font></td> </tr> ROW; } } // End of the WHILE loop. mysql_close(); // Close the database connection. // Print the footer, close the table, and the form. echo ' <tr><td></td><td></td><td></td><td></td><td></td><td></td> <td align="right"><font face="arial" size="2" color="white"><b>Total:<b></font></td> <td align="right"><font face="arial" size="2" color="white"><b>R' . $total . '</font></td> </tr> </td></tr> </table><table border="0" width="700" cellspacing="3" cellpadding="3"> <tr> <td width="700" align="center"> <font face="arial" size="2" color="white"><b>This order excludes postage & packaging and VAT</b></font> </td> </tr> <tr> <td> </td><td> <form action="pay.php" method="post"> </tr> </table> '; print <<<DETAILS2 <table align="center"> <tr><td> <input type="image" name="submit" src="images/enter_details.jpg"></form></td><td> <form action="order.php" method="post"><input type="image" name="submit" src="images/another_photo.jpg"></form></td></tr> </td> </tr> </table></table></table><tr> DETAILS2; // Register the total to the session. $_SESSION['total'] = $total; // Display the form. ?> </td> </tr> </table> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/#findComment-650237 Share on other sites More sharing options...
Psycho Posted September 25, 2008 Share Posted September 25, 2008 Well, if you want help it is extremely helpful if you 1) use code tags when posting and 2) have properly indented code to visually display the structure. In any event, the solution is still the same as I stated above. Since, you are displaying the form again after a submission you need to just reload the page WITHOUT the post data using a header() function. You will need to move all of your processing code above any output to the browser. Then after the processing is done (e.g. Inserts) and before you output anything to the page add this command: header('Location: '.$_SERVER['PHP_SELF']); The page will then reload as if no data was submitted. So the user can hit refresh as much as they like and no duplicates will be entered. Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/#findComment-650542 Share on other sites More sharing options...
Psycho Posted September 25, 2008 Share Posted September 25, 2008 Forgot to add that you will also need to do a check to ONLY do the redirect if data was submitted if (isset($_POST['submit'])) { header('Location: '.$_SERVER['PHP_SELF']); } But you should be doing that check to determine if you should process the data anyway, so you could simply add it at the end of the processing phase. Quote Link to comment https://forums.phpfreaks.com/topic/125740-double-db-entries/#findComment-650559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.