I have 3 tables: user, student, studentdetails
User's primary key is the foreign key for a field in table student (userid) and student's primary key is foreign key for a field in table studentdetails (studentid).
I need to insert data from one form to all 3 tables in one submit, following is the SQL script:
$sql = "
INSERT INTO `tbluser`(`username`, `password`, `roleid`, `datecreated`)
VALUES ('$email','$password', '$role', CURRENT_TIMESTAMP);
SELECT @uid := MAX(`userid`) FROM `tbluser`;
INSERT INTO `tblstudent` (`userid`, `scholarnumber`, `firstname`, `middlename`,
`lastname`, `datecreated`)
VALUES ('@uid', '$scholar_no', '$first_name', '$middle_name', '$last_name',
CURRENT_TIMESTAMP);
SELECT @stuID := MAX(`studentid`) FROM `tblstudent`;
INSERT INTO `tblstudentdetails` (`studentid`,`dob`, `studenttype`, `gender`,
`religion`, `category`, `currentsuburbid`, `currentcityid`, `currentaddress1`,
`currentzipcode`, `currentstateid`, `currentcountryid`,`mobile`,`phone1`,`email1`,
`passportnum`, `permasuburbid`, `permaaddress1`, `dateofjoining`,
`admissionreferenceof`, `datecreated`, `dateupdated`)
VALUES ('@stuid', '$dob' ,'$studenttype' ,'$gender','$religion','$category',
'$currentsuburbid', ' $currentcityid', '$currentaddress1', '$currentzipcode',
'$currentstateid', '$currentcountryid', '$mobile',
'$phone1','$email1','$passportnum','$permanentsuburbid', '$permanentaddress1',
'$doj', ' $admissionreference',current_timestamp, current_timestamp);
";
I am not able to figure out the problem, the above script works in mysql (phpmyadmin) but in php it doesn't work. I understand, I need to use multi_query (??) which I am but it doesn't give any error and inserts in two tables, but doesn't in the third one. I feel it might be to do with the SELECT statement in between? At wits end here, I would greatly appreciate any help. Thanks heaps in advance.