我是新手,我正在尝试将数据插入多个表
$prisoner_insert = "INSERT INTO prisoner (givenName,middleName,lastName,nickname,presentAdd,permanentAdd,prisonno,age,
gender,civilstatus,birthdate,birthplace,height,weight,build,eyecolor,haircolor,complexion,nationality,religion,acrno,
education,occupation,motherName,fatherName,spouse,childno,nearestkin,relationkin,kinAdd,status)
VALUES ('$gname','$mname','$lname','$aname','$padd','$pedd','$pno','$age','$gender','$cstatus','$bdate','$bplace',
'$height','$weight','$build','$ecolor','$hcolor','$complex','$nation','$relign','$acr','$edu','$occu','$mother','$father','$spouse',
'$child','$kname','$krelation','$kaddress','$status')";
$offense_insert = "INSERT INTO offensedata (crime,crimedate,caseno,trialcourt,date_sentence,datetime_commited,imprisonment)
VALUES ('$c_com','$c_date','$c_caseno','$trialc','$d_commence','$dt_committed','$imprisonment')";
if(mysqli_multi_query($prisoner_insert, $offense_insert)){
echo "Inserted Successfully!";
}
else{
echo "Error:" . "<br>" . $connect->error;
}
我收到以下错误:
mysqli_multi_query()期望参数1为mysqli,字符串为 C:\ XAMPP \ htdocs中\ jailbsystem \管理员\ admin_add.php
答案 0 :(得分:-2)
您需要在db link
的第一个参数处传递mysqli_multi_query();
,如下面的代码所示。
此外,您需要以正确的方式组合您的SQL查询。使用;
作为每个命令的分隔符。
删除了之前的变量$prisoner_insert
,$offense_insert
,并将其替换为只有一个$jail_sql
。
来自offensedata
INSERT的prisoner_id
未提供prisoner
的INSERT。作为修复,在offensedata中添加了LAST_INSERT_ID()作为字段prisoner_id
的值;
LAST_INSERT_ID()
提供整个数据库连接最近自动生成的ID的ID。
更新代码:
$jail_sql = "INSERT INTO prisoner (givenName,middleName,lastName,nickname,presentAdd,permanentAdd,prisonno,age,
gender,civilstatus,birthdate,birthplace,height,weight,build,eyecolor,haircolor,complexion,nationality,religion,acrno,
education,occupation,motherName,fatherName,spouse,childno,nearestkin,relationkin,kinAdd,status)
VALUES ('$gname','$mname','$lname','$aname','$padd','$pedd','$pno','$age','$gender','$cstatus','$bdate','$bplace',
'$height','$weight','$build','$ecolor','$hcolor','$complex','$nation','$relign','$acr','$edu','$occu','$mother','$father','$spouse',
'$child','$kname','$krelation','$kaddress','$status');";
$jail_sql .= "INSERT INTO offensedata (prisoner_id, crime,crimedate,caseno,trialcourt,date_sentence,datetime_commited,imprisonment)
VALUES (LAST_INSERT_ID() , '$c_com','$c_date','$c_caseno','$trialc','$d_commence','$dt_committed','$imprisonment');";
if(mysqli_multi_query($conn, $jail_sql)){
echo "Inserted Successfully!";
}
else{
echo "Error:" . "<br>" . $connect->error;
}