我有 3 个表:用户、学生、学生详细信息
用户的主键是表student(userid)中某个字段的外键,学生的主键是表studentdetails(studentid)中某个字段的外键。
我需要在一次提交中将数据从一个表单插入到所有 3 个表中,以下是 SQL 脚本:
$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);
";
我无法找出问题所在,上面的脚本在 mysql (phpmyadmin) 中工作,但在 php 中它不起作用。我了解,我需要使用我的 multi_query (??),但它不会给出任何错误并在两个表中插入,但不会在第三个表中。我觉得这可能与两者之间的 SELECT 语句有关?在这里斗智斗勇,我将不胜感激任何帮助。提前致谢。