in reply to Re^3: (CGI) Prevent a reload from resubmitting a form
in thread (CGI) Prevent a reload from resubmitting a form

Maybe i dont get the point, but i thought this was what i tried to do with this script: Passing an unique number to the form, check if it was send bevor, if it was do not write into the db again.
Or do i understand something wrong?
Sorry, my english is not that good.
  • Comment on Re^4: (CGI) Prevent a reload from resubmitting a form

Replies are listed 'Best First'.
Re^5: (CGI) Prevent a reload from resubmitting a form
by Anonymous Monk on Jun 27, 2009 at 14:17 UTC

    The unique number could serve as a session identifier. If the basic constraint is that you want to carry out just one database insert for each unique id, you need to make sure that the session id generation mechanism can't be triggered to hand out fresh ids when unwanted and also that no two different users will get the same id.

    As for the other part - transactions - you need to ensure that no matter how many times a request (with the same session id) is submitted, the insert is carried out just once. That can be done in a number of different ways, a simple one is this (pseudocode) - having a primary key on session_id in table:

    sub my_request { my($session_id, $data) = @_; eval { <insert $session_id, $data into table> }; if($@) { if($@ ne primary_key_violation) { <Handle error> } # No else, since the primary_key_violation # just means that the data has been inserted # by another request } <Print everything is okay> }

    The main catch in the code above is not to treat it as an error condition if you discover that the data has already been inserted. Depending on the way you initialize DBI and the driver, your may get an exception when violating a constraint. If you can't get it to throw an exception, you can check your statement handle for any errors.

    Remember to encapsulate everything in a database transaction if you carry out multiple database operations. By doing this, you can do a rollback if you detect that another request has inserted the data already.

      (Sorry - the above post was just me again. I forgot to log in.)

      Consider looking a little closer at this term:

      Idempotent (Wikipedia).