MySQL Forums :: PHP :: Insert ID from one table to another?
Insert ID from one table to another?
Posted by: karl williams
Date: December 06, 2013 03:54PM
Im trying to find a way to update an ID from one table to another. The tables i have are listed below:
TutorID (PRIMARY KEY - AUTO INCREMENT)
ModuleID (PRIMARY KEY - AUTO INCREMENT)
TutorID (FOREIGN KEY - REFERENCES Tutors.TutorID)
I want on INSERT for TutorID from Tutors to update TutorID in Modules, so as a Tutor can have many Modules assigned to them. I have tried many ways and for days to try and find a way. Then i thought i had it with the code i will post below, with this code i get no Errors....but when i look at the Data, only Tutors has any Values in it. Modules stays empty. Thankyou for any help with this problem.
$sqlstatement ="INSERT INTO Tutors VALUES ( 'NULL', '$name', '$roomno')";
$sql_result = mysql_query($sqlstatement, $connection)or die ("Could not insert into the DATABASE1");
$sqlstatement ="SET @last_id_in_Tutors = LAST_INSERT_ID()";
//ISSUE AND SQL INSERT INTO THE MODULES TABLE
$sqlstatement ="INSERT INTO Modules VALUES ( last_id_in_Tutors(), '$title', '$modcode', '$hours')";
$sql_result = mysql_query($sqlstatement, $connection)or die ("Could not insert into the DATABASE!!2");
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.