Fellow Monks, I have searched high and low, brushed the dust off of my copy of "Programming the Perl DBI", and even ventured into the confines of Super Search. Unfortunately, all of my searching has been for naught, as my question remains unanswered. Either my eyes are failing me in my dotage or...Well, I certainly doubt that I'm the first person to ever want this question answered. Perhaps I'm just using the wrong search terms. I have a web site that takes customer input (name, address, email, etc.) and tosses the information into a simple database table. A simplified version of this table looks like:
CUSTOMER_INFORMATION -------------------- id (auto-incrementing sequence, not null) first_name last_name address_1 address_2 city state country postal_code ...etc...
After validating and un-tainting user input, I use the DBI to insert the information into my Postgres table like this:
$dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1, PrintError => 1, AutoCommit = +> 1 }); $sql = $dbh->prepare_cached(' INSERT INTO Customer_Information ( First_Name, Last_Name, Address_1, Address_2, City, State, Postal_Code ) VALUES ( ?, ?, ?, ?, ?, ?, ?); '); $sql->execute($FName, $LName, $Address1, $Address2, $City, $State, $PostalCode);
This code works (well, ok...The real code that I'm executing in my real script works. There might be some translation errors from script to PerlMonks in what's above.) What I'd like to know is the value of the "ID" column for the row that was just inserted. I have done this in other scripts by using what feels to me like a hack:
select max(id) from customer_information where first_name = $FName and + last_name = $LName;
Store the result of that into a variable, and voila, I have the ID I'm looking for. I need this ID to insert as a foreign key into another table immediately after this insert is run. Is there a better way to do this? Thanks kindly.

In reply to Getting ID of last row inserted into database by Yendor

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.