Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Transactions using php and mysql problem

Posted on 2005-05-16
Medium Priority
Last Modified: 2013-12-12
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?
Question by:Chriseybaby
  • 2
  • 2

Expert Comment

ID: 14009334
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.

Author Comment

ID: 14009354
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?

Accepted Solution

JKlatte earned 2000 total points
ID: 14011427
Well, you can still stop the room from being double booked if you use a transaction with the final confirmation. I don't think it'll happen very often that an identical booking is made in the few seconds it takes to press a 'confirm'-button, but should this happen, the transaction can return an error message instead of making a double booking.

Author Comment

ID: 14011628
Thank you for your help and for being patient with me. Enjoy the points

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question