Results 1 to 7 of 7
May 10, 2011, 02:45 #1
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
May 10, 2011, 02:57 #2
May 10, 2011, 03:02 #3
Thank you for correction just noted that...and again how do i use that function
May 10, 2011, 03:08 #4
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).
May 10, 2011, 03:12 #5
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();
$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]')";
$result1 = mysql_query($sql2) or die(mysql_error());
May 10, 2011, 03:15 #6
May 10, 2011, 03:19 #7
Thank you very much......it works now