Question

the best way to explain my question is with an example

say i have 3 scripts;

first one is a form, on submitting this it goes to second script which processes the POST variables and Inserts them into the DB on this page/script there's another submit button, that takes you to the third page where the Insert query is finally commited to the DB

is this possible?

or do commit/rollback have to be on the same script?

thanks

Was it helpful?

Solution

Yes, commit/rollback has to be in the same request that started the transaction.

Another way of looking at this is that transactions must be resolved within the same database connection, and database connections (like any other resource) don't survive across multiple PHP requests.

As @Wrikken comments, you could save the uncommitted data in session data, or some other non-database holding space (e.g. memcached).

Another option is to save and commit the data in the database during each request, but add a column to your table for the state of the data. Therefore it would be physically committed with respect to database transactions, but it would be annotated as incomplete until you finish handling the third script.

I've implemented systems like this, for example for "shopping cart" style information. It also helps to run a daily cron job to delete old, unfinished data. Because inevitably, people do sometimes abandon their work in progress and never get to the finish step.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top