php retrieving id of the last record inserted and insert it in another table

Posted by Mauro74 on Stack Overflow See other posts from Stack Overflow or by Mauro74
Published on 2010-05-31T23:05:00Z Indexed on 2010/05/31 23:13 UTC
Read the original article Hit count: 293

Filed under:
|

Hi all,

I'm trying to retrieve the id of the last record inserted in the table 'authors' and insert the retrieved id in the 'articles' table; here's my code:

$title = mysql_real_escape_string($_POST[title]);
$body = mysql_real_escape_string($_POST[body]);
$category = mysql_real_escape_string($_POST[category]);

$insert_author="INSERT INTO authors (name) VALUES ('$title')";
$select_author= mysql_query("SELECT LAST_INSERT_ID()");

$authId = mysql_fetch_array($select_author);
$query="INSERT INTO articles (body, date, category, auth_id) VALUES ('$body', '$date_now', '$category', '$authId')";

this doesn't work...

Any idea please?

Thanks in Advance

Mauro

© Stack Overflow or respective owner

Related posts about php

Related posts about lastinsertid