Super frustrating: row inserts twice

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

mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Super frustrating: row inserts twice

Post by mattpointblank »

Okay, before you ask, I'm fairly (95%) certain that the error isn't in my query itself.

I'm writing a web shop. Here's the flow:


User chooses a product from shopitems.php
User is sent to additem.php?id=123, which adds some info to the database then uses header() to redirect them to the shopping basket page.

I set up additem.php to email me when a record is added. I get two emails - the first one has shopitems.php as its referrer, the other doesn't have a referrer.

If I remove the header call to shoppingbasket.php, the row is only inserted twice. But here's the catch: if I remove all the code from shoppingbasket.php and then redirect there, it still inserts the two rows and I still get two emails - this suggests that the INSERT query code isn't duplicated anywhere else.

Why on earth would calling header() at the end of a script cause it to run a second time? I can confirm that I don't include that file anywhere else or redirect to it from anywhere.

Code:

additem.php

Code: Select all

 
<?php 
$query1 = "SELECT ProductPriceID FROM tblProductsPrice WHERE ProductID = '" . cleanGet("id") . "'";
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_row($result1);
 
$uniqueUserID   = $_SESSION['uuid'];
$productID      = cleanGet("id");
$productPriceID       = $row1[0];
$userIP                 = $_SERVER['REMOTE_ADDR'];
 
 
$query2 = "INSERT INTO tblShopTransactions (UserID, ProductID, ProductPriceID, NumberItems, SessionActive, ClientIP, DateAdded) 
          VALUES ('$uniqueUserID', '$productID', '$productPriceID', '1', '1', '$userIP', CURRENT_TIMESTAMP())";
$result2 = mysql_query($query2) or die(mysql_error());
 
// code to email me goes here
 
session_write_close();
header("Location: shoppingbasket.php");
exit;
?>
 
Anyone?
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Super frustrating: row inserts twice

Post by it2051229 »

or maybe the "additem.php" is being called twice.... hmmm mind checking that for me so we can cross it out if it's not...
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

I ran a search for the script filename additem.php across my entire site - the only place it occurs is in the shop index, where each product links to it, once.

Interestingly, there's normally at least a second (sometimes a lot more when my server is sluggish) between the two rows being inserted.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Super frustrating: row inserts twice

Post by pickle »

After the insert query is called, place this:

Code: Select all

echo '<pre>';
print_r(debug_backtrace());
echo '</pre>';
This will show you a history of what functions & files were called in order to get there. That might be useful in determining if the file is being called multiple times, and from where.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

Just gives me

Array
(
)


:(

There aren't any custom functions being used and the only included files are my database connection stuff and a utility class that isn't even called here. This is so strange.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Super frustrating: row inserts twice

Post by pickle »

There's no way that should be empty. At the very least it should have information about the current file.

Try calling exit() after debug_backtrace(). Then see if the row gets inserted twice.

What browser are you using?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

Nope, still does it. Using Firefox but it does the same in IE6 too.

It doesn't add 2 rows when I don't use the header() function though.
Tobey
Forum Newbie
Posts: 12
Joined: Thu May 14, 2009 11:40 am
Location: Germany

Re: Super frustrating: row inserts twice

Post by Tobey »

Can you show us the complete code of additem.php? And if you don't want it to be included more than once, or if you just want make sure, you can add the following at the beginning of the code:

Code: Select all

<?php
if(defined("ADDITEM_INCLUDED")) die("additem.php");
else define("ADDITEM_INCLUDED", 1);
?>
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

It's essentially what I posted above:

Code: Select all

 
<?php 
require_once '_includes/webhelpers.php'; // contains connection stuff, used everywhere else on site too
require_once('shopFunctions.php'); // contains code to generate $_SESSION['uuid']
 
$query1 = "SELECT ProductPriceID FROM tblProductsPrice WHERE ProductID = '" . cleanGet("id") . "'";
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_row($result1);
 
$uniqueUserID   = $_SESSION['uuid'];
$productID      = cleanGet("id");
$productPriceID       = $row1[0];
$userIP                 = $_SERVER['REMOTE_ADDR'];
 
 
$query3 = "INSERT INTO tblShopTransactions (UserID, ProductID, ProductPriceID, NumberItems, SessionActive, ClientIP, DateAdded) 
          VALUES ('$uniqueUserID', '$productID', '$productPriceID', '1', '1', '$userIP', CURRENT_TIMESTAMP())";
$result3 = mysql_query($query3) or die(mysql_error());
 
    $message = "item $productID added";
    $headers = "From: me@site.com";
    mail("me@site.com", "item added", $message, $headers);
 
// WHY DOES THIS REDIRECT DUPLICATE THE INSERT?!
session_write_close();
header("Location: shopgetbasket.php");
exit();
 
?>
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

A little more information:

After adding an item to the cart, the header() function sends the user to shoppingcart.php which shows them their items. When this page loads, it only shows 1 order for the product. When you reload the page, it shows 2. Or, if I look in the database as soon as this page is finished loading, it shows 2 rows. The difference in seconds between the time of the two row inserts suggests that one row is inserted when the user clicks to buy the product, and the second is added AFTER the shopping basket shows the user their purchases, which is why (at the time of loading) it only shows 1 order.

The strange part? All of this would suggest that there's something in shoppingcart.php adding another row AFTER the cart is displayed. However, if I completely remove all the code from shoppingcart.php and try to process the order, it still adds 2 rows. And again, my mail() function in the additem.php script is being called both times, so the code is definitely being executed there.

This is so bizarre and annoying.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Super frustrating: row inserts twice

Post by pickle »

Try redirecting somewhere that isn't shoppingbasket.php & see what happens.

& just to double check - you've confirmed that if you comment out that header line, then it doesn't get inserted twice?

It's possible that if you have Firebug installed, that it's submitting the form twice (it's happened to me). However, you said it's happening in IE6 as well, which basically throws that theory out the door.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Super frustrating: row inserts twice

Post by it2051229 »

i agree.. looks like shoppingbasket.php has something to do with calling the additem.php twice...
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

Yep - I tried redirecting to another page, still happens. Confirmed that commenting out the redirect fixes the problem. Could this be a bug...?

Also, it's not even a form, you just click on an 'add to basket' link which takes you to the page. It still happens even if I access the page directly (eg additem.php?id=123).
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

Did a bit of searching, there's this: http://bugs.php.net/bug.php?id=8382

Doesn't really correspond, but made me think about sessions - I was getting a session already started warning, so I disabled the one that starts in my included files and hardcoded the unique user ID (which was previously generated from MySQL) - still happens. So weird.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Super frustrating: row inserts twice

Post by mattpointblank »

Could anyone try making a really bare bones version of this and see if it does the same thing for them too?
Post Reply