1
votes

I'm looking to use SELECT LAST_INSERT_ID() Am using a form to have a user input values. With the first insert I need to get the last inserted id for the next insert... I have not figured out how to get the last selected id and then pass it into my 2nd insert statement

I have updated my code though I still can not get the id to post into the table

include("config.inc.php");
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
$query = "INSERT into `".$db_table."` (producer_id,series_id,lang_id,title_name,title_public_access) VALUES ('" . $_POST['producer_id'] . "','" . $_POST['series_id'] . "','" . $_POST['lang_id'] . "','" . $_POST['title_name'] . "','" . $_POST['title_public_access'] . "')";

$last_id = mysql_insert_id();

$query = "INSERT into `".$db_table2."` (seg_id, file_video_UNC,file_video_URL) VALUES ('" . '$last_id' . "','" . $_POST['file_video_UNC'] . "','" . $_POST['file_video_URL'] . "')";

mysql_query($query);
mysql_close($link);
3
Not sure how appropriate of a "question" this is... Looking at your last question, I'd suggest looking into some basic MySQL/PHP tutorials out on the web before getting someone to piece together something for you. - brianreavis
Or buying a decent book. You can only get so far with online "tuts"; usually in the wrong direction. - Lightness Races in Orbit
@Tomalak, I couldn't disagree more. Everyone has their own learning patterns, and maybe books work for you, but not everyone. I've read plenty of crap and inaccuracies in published books, just as I have online. - Brad
@Brad I never said that every book out there is infallible. But I also don't think it takes a genius to realise that a recommended PHP or MySQL book is light-years more trustworthy than a tutorial that some spotty 13-year old has posted on his blog. - Lightness Races in Orbit
@Tomalak, 'eh, perhaps. I've seen a few recommended horrible books, just as you have seen plenty of horrible tutorials online. I'd also point out that age makes no difference on these things. I know a couple 50+ aged folks who have terrible tutorials on their blogs/websites. We were all there once, yes? It is important to encourage younger generations rather than bashing them online as you just did. - Brad

3 Answers

2
votes

There's a function for that, called mysql_insert_id().

... first query here ...
$last_id = mysql_insert_id();
$sql = "INSERT INTO $db_table SET 
    file_video = " . $_POST['file_video_UNC'].",
    file_video_URL = " . $_POST['file_video_URL'] . ",
    insert_id_of_first_query = $last_id";
...

Your updated code doesn't send the query to database - as a result no INSERT, so no LAST_INSERT_ID

$query = "INSERT into ".$db_table." 
    (producer_id,series_id,lang_id,title_name,title_public_access) VALUES
    ('" . $_POST['producer_id'] . "','" 
        . $_POST['series_id'] . "','" 
        . $_POST['lang_id'] . "','" . $_POST['title_name'] . "','" 
        . $_POST['title_public_access'] . "')";

mysql_query($query); /* YOU FORGOT THIS PART */
$last_id = mysql_insert_id();
1
votes

You can't just dump a query into a string on its own in a line of PHP. You should have used LAST_INSERT_ID() inside your second query or, better, use PHP's mysql_insert_id() function which wraps this for you in the API.

0
votes

In the line:

$query = "INSERT into `".$db_table2."` (seg_id, file_video_UNC,file_video_URL) VALUES ('" . '$last_id' . "','" . $_POST['file_video_UNC'] . "','" . $_POST['file_video_URL'] . "')";

I think VALUES ('" . '$last_id' . "', should just be VALUES ('" . $last_id . "', without the single quotes around the variable.