Link to home
Start Free TrialLog in
Avatar of Chriseybaby
Chriseybaby

asked on

Transactions using php and mysql problem

I am attempting to use transactions on my website using php and mysql.

I have set autocommit=0
and have started the transaction

The problem is that I would like the user to be able to click either a confirm or cancel button, where if the confirm button is clicked i Commit the transaction and if the cancel button is clicked it rollsback the transaction.

The only trouble is in my form im using enctype=multipart/form-data method=POST so the page reloads when the button is clicked.

From my knowledge this means the mysql connection is lost so I now cannot commit or rollback the transaction.

When I do reload the page and reconnect to the DB I can get the inserted id but there is no field in the table.

Any comments on other ways of doing this or where im going wrong?
Avatar of JKlatte
JKlatte

This is not what transactions are meant to do. If you'd like a confirm/cancel button set, it is not necessary to start the query before the button is clicked  - the transaction (or simple query) comes afterwards - or not, in case 'cancel' is clicked.
Transactions are only useful if you wish to run multiple queries in a set, in such a way that it is not possible for one query to run and another to fail (for example, for a cash transaction: person A sends money to person B, so the balance of A is reduced AND the balance of B is increased - it shouldn't be possible that the money disappears, like if the money is deducted from A and never added to B because of a db error).

In your situation, I'd simply store the info that you need for the query in a session or form variables, so you can show what is going to happen if the users clicks 'confirm', and start running the query or complete transaction after the 'Confirm' button is clicked.
Avatar of Chriseybaby

ASKER

The idea of the website is to book rooms. What i was trying to do was use transactions so I could run the query after the user had selected the dates, this would stop the room being double booked on those dates, and then the user can confirm (commit) the booking or cancel(rollback)

Is there a way of doing so, even though it may not be the purpose of transactions?
ASKER CERTIFIED SOLUTION
Avatar of JKlatte
JKlatte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for your help and for being patient with me. Enjoy the points