开发者

SQL Query seems to be executing twice [duplicate]

开发者 https://www.devze.com 2023-04-08 05:27 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Insert query on page load, inserts twice?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Insert query on page load, inserts twice?

I'm hoping someone can help with a problem that's driving me up the wall. I've searched but found nothing directly answering this.

I have a problem on a website where an SQL INSERT query seems to run twice but only on some occasions.

It's a simple shopping site. On my product page there is an 'Add To Cart' button. On pressing this a user is taken to the Cart page, and various variable are passed a开发者_如何学JAVAcross. An INSERT query is run when the page loads and the results are displayed in the shopping cart.

This all works fine most of the time, but on some occasions, the product is added to the database twice.

The problem is not related to any specific product. Since it works OK the majority of the time, I'm pretty confident the code is OK. The only thing I can say is that I have not as yet seen the problem outside of Firefox. Some people have suggested turning of the 'Disable Cache' via Toolbar, which I have, but still getting problems.

Very grateful for any suggestions.


What happens when you reload the cart page? Does the POST operation happen again - it could be that if there's a hang while the page is loading (which can happen with Firefox - particularly on Macs - something to do with its caching system) the user might be hitting reload and therefore resending the page data and running in INSERT query again.

IF this is the case...

... the best idea is probably to add in a step that deals with the database that outputs nothing (no output means no caching). So your "add to basket" form posts the data to a database handling script which performs the operations and then does a simple header("location:...") to pass the user off to the "display cart" page.

That way the script that performs the database operations displays nothing and is never cached - and the user can hit reload on the "display cart" page as often as they like and it'll just reload their existing cart.


Firstly, though it's not going to sound very helpful, the problem will be with your code, not with Firefox, PHP, or MySQL. I've been through exactly what you're going through, and been sure it must be a bug, but something this obvious would have been fixed by now.

To help you narrow down where the bug is occurring, it would be worth logging various points in your code, so you can see how it flows through. It may help to log the SQL queries as well, so you can see if they are being called in quick succession, or whether it is a loop of some sort where something else occurs before the second call.

As the insert is done in PHP, this is all carried out server-side, so the chances of it being browser-specific are pretty slim.

There's a lot of help on this site, but to give you any more concrete advice you'll need to post code, and a bit more detail about the flow of the application.

Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消