Insert data into a child table using mysql_insert_id


Can someone please help me with using the mysql_insert_id function in mysql…I have two tables in which one is child to other and I am trying to insert data from the same form but the data has to be separated into those two tables.

I could only get the data in one table but in the child table i could not update the value of the foreign key. instead i get an error FUNCTION mydb.mysql_insert_id does not exist

Any help will be appreciated…thank you:)

mysql_insert_id is not a mysql function, but a php function: PHP: mysql_insert_id - Manual

Thank you for correction just noted that…and again how do i use that function

Did you take a look at the manual and the examples there?
You do the insert in the main table, then get the inserted id, then use that value for the insert in the second table (the one with the foreign key).

Yes I have and here is what I have been trying to use it…

//prepare query to insert data into table drivers
$sql = “INSERT INTO drivers(driverRegNo,schoolId, surname, othernames, DOB, address, gender, placeOfBirth, telephoneNo) VALUES (‘$driverRegNo’,‘$schoolId’, ‘$surname’, ‘$othernames’, ‘$DOB’, ‘$address’, ‘$gender’, ‘$placeOfBirth’, ‘$telephoneNo’)”;
$driver_Id = mysql_insert_id();
//process query
$result = mysql_query($sql) or die(mysql_error());

//query to insert data into second table
$sql2= “INSERT INTO licence (issueDate, driverID,licenceClass, issuingAuthority, expiryDate) VALUES (‘$_POST[issueDate]’, ‘$driver_Id’, ‘$_POST[licenceClass]’,‘$_POST[issuingAuthority]’,‘$_POST[expiryDate]’)”;

//process query
$result1 = mysql_query($sql2) or die(mysql_error());

Put this line

$driver_Id = mysql_insert_id();

AFTER you’ve executed the first query.

Thank you very much…:)…it works now :smiley: