Transactions using php and mysql problem

Posted on 2005-05-16
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
    LVL 4

    Expert Comment

    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

    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?
    LVL 4

    Accepted Solution

    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

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

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    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 …

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now