Many times there is a need to get the autogenerated ID from the last MySQL INSERT performed.
This ID can then be used to perform some more INSERTs particularly in different tables.
As a typical example lets assume you have members table and a seperate table to store their preference for receiving newsletter.
When a new member registers, we perform the first INSERT to add the member details in the member table.
Now our newsletter table needs the memberID generated from this insert so that we can store their preference (In case you are using their memberID as the primary key instead of the username)
They way to perform this is as below
$query1="INSERT INTO tblmember (username, password, salutation, firstname, lastname)
VALUES ('$username', '$password', '$salutation', '$firstname', '$lastname')";
$memberid = mysql_insert_id();
$query2="INSERT INTO tbmailinglist (memberid, newsletter) VALUES ('$memberid','1')";