1

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);
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
Droid646197
  • 65
  • 2
  • 2
  • 8
  • 1
    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 Jan 13 '11 at 00:29
  • Or buying a decent book. You can only get so far with online "tuts"; usually in the wrong direction. – Lightness Races in Orbit Jan 13 '11 at 00:30
  • @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 Jan 13 '11 at 00:40
  • @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 Jan 13 '11 at 00:43
  • 1
    @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 Jan 13 '11 at 00:48
  • @Brad: Again, you're employing a logical fallacy. The veracity of blogs written by 50+ year olds has nothing to do with my statement about 13 year olds, and the fact that "we were all there once" and the indeed desirable patience with the younger generation does not make information that they have posted at the very beginning of their intellectual career any more accurate. And of course there are horrible books too; in my opinion, buying an arbitrary book vs reading an arbitrary tutorial, you're far more likely to have picked a decent resource if you went for the book. – Lightness Races in Orbit Jan 13 '11 at 00:52
  • Thank you for the input.. i have some grasp.. though if you all have a good book i would take it am for learning in all forms i kind of got stuck with this. Again thank you all for your help – Droid646197 Jan 13 '11 at 01:11

3 Answers3

2

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();
German Rumm
  • 5,604
  • 1
  • 22
  • 30
  • I have updated dated my code and still not pulling the id for me. – Droid646197 Jan 13 '11 at 04:58
  • Code in your updated answer doesn't execute a query. You need to execute `mysql_query($query)` first. I will update my answer accordingly – German Rumm Jan 13 '11 at 08:22
  • 1
    I know this is just an example, but shoving $_POST data directly into your database.... you may as well just email your database to every Russian and Chinese hacker out there! (no offense). – Yevgeny Simkin Aug 29 '12 at 06:54
  • @Dr.Dredel, I totally agree, also nobody should be using `myqsl_` functions now, since they are deprecated and all. I just copied the code of the OP, so that the missing part will be clearly visible. – German Rumm Aug 29 '12 at 07:11
1

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.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
0

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.

riot
  • 177
  • 2
  • 7