Jump to content

double db entries


lional

Recommended Posts

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);

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.