ysth writes:
You could insert the row with a "not yet published" flag, and only create the file after the insert.
Instead of keeping the image data in a file, consider storing it in the database in the same row that describes it. Keeping a file system and database in sync can tricky. You have to be prepared to handle files which don't have database entries and database entries which don't refer to files. That's why it can be simpler just to store your file data in your database (e.g. in a BLOB column.) Just something to think about...

Also, to avoid the race condition that ysth refers to, try to perform the determination of the last number used and the updating of the new row in one SQL statement:

-- create the new image row with blank gallery_id: INSERT INTO images ... VALUES (...); -- atomically update the gallery_id field UPDATE images SET gallery_id = (SELECT 1+MAX(...) FROM images) WHERE + ...;
Another possibility is to do it all in one INSERT statement:
INSERT INTO images (gallery_id, title, description, fstop) SELECT 1+MAX(...), 'title', 'description', 'fstop' FROM images;
You'll run into a race condition if you have perl figure out the next id and then use that id in a subsequent INSERT or UPDATE statement.

In reply to Re^4: How to find the highest number in a mysql table by pc88mxer
in thread How to find the highest number in a mysql table by Anonymous Monk

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.