Skip navigation links

MySQL Forums :: PHP :: Insert ID from one table to another?


Advanced Search

Insert ID from one table to another?
Posted by: karl williams ()
Date: December 06, 2013 03:54PM

Hi Guys

Im trying to find a way to update an ID from one table to another. The tables i have are listed below:

Tutors_table
TutorID (PRIMARY KEY - AUTO INCREMENT)
TutorName
RoomNumber

Modules_table
ModuleID (PRIMARY KEY - AUTO INCREMENT)
TutorID (FOREIGN KEY - REFERENCES Tutors.TutorID)
Title
ModCode
Hours

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");

Rgds Willo

Options: ReplyQuote


Subject Written By Posted
Insert ID from one table to another? karl williams 12/06/2013 03:54PM
Re: Insert ID from one table to another? Peter Brawley 12/06/2013 08:06PM


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.