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.


In reply to Re^5: (CGI) Prevent a reload from resubmitting a form by Anonymous Monk
in thread (CGI) Prevent a reload from resubmitting a form by fmk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.