美好的一天,
因与库MySQLi多重插入困难。 我有这个分贝它由6页不同的表每个它具有eid
为foreign key
的employees
表。 下面是表列表: employees
(其中ofcourse持有primary key
的eid
),再contact
, education
, job_desc
, work_history
, familybg
什么时遇到的问题是,当我试图从插入到其中的MySQLi函数被调用了insert.php形式的索引页的拍摄数据。 你可以看到下面的代码:
[注:下面的属性仅仅是虚设。]
insert.php(修订版)
include('db.php');
//build sql statements
<?php
$sql1 = "INSERT INTO employee (fname,mname,
lname,age,
gender,birthday,
birthplace,citizenship,
status,sss,
philhealth,tin,
height,weight)
VALUES
('$fname','$mname','$surname',
'$age','$gender',
'$birth','$place',
'$citizen','$civil',
'$ss','$phil','$tin',
'$height','$weight')";
$r1 = mysqli_query($db,$sql1);
$id = mysqli_insert_id($db);
$sql2 = "INSERT INTO contact (eid,address,province,postcode,telno,mobile,email,alternate)
VALUES('$id','$address','$province','$postcode','$tel','$mob','$email','$alter')";
$sql3 = "INSERT INTO educ (eid,elem,egrad,highschool,hgrad,college,cgrad) VALUES ('$id','$elem','$egrad','$high','$hgrad','$col','$cgrad')";
$sql4 = "INSERT INTO employment (eid,position,status,hireDate,job_desc,basic,salary) VALUES ('$id','$pose','$stat','$hstart','$dept','$pay','$salary')";
$sql5 = "INSERT INTO work (eid,company_name,position,desc,startDate,endDate) VALUES ('$id','$prev','$pold','$job','$sdate','$edate')";
$sql6 = "INSERT INTO family (eid,fatherName,motherName,sibling,spouse,children) VALUES ('$id','$dad','$mom','$kname','$spouse','$child')";
//returns 1 if sucessful and 0 if not
//mysqli_query($link,$query)
$r2 = mysqli_query($db,$sql2) or die(mysqli_error($db));
$r3 = mysqli_query($db,$sql3) or die(mysqli_error($db));
$r4 = mysqli_query($db,$sql4) or die(mysqli_error($db));
$r5 = mysqli_query($db,$sql5) or die(mysqli_error($db));
$r6 = mysqli_query($db,$sql6) or die(mysqli_error($db));
$sqlResult = $r2 && $r3 && $r4 && $r5 && $r6;
//commit the queries if no errors otherwise rollback
if(!$sqlResult){
//sqlResult = 0, thus there was a problem
mysqli_rollback($db);
echo "ERROR: Could not save data!";
}else{
//sqlResult = 1, no problem
mysqli_commit($db);
echo "Record saved!";
}
mysqli_close($db);
?>
输出显示: ERROR: Could not save data!
当我试图运行该程序。 它只是意味着insert语句无法继续,从而返回到mysqli_rollback(); 起初我以为也许这有什么做的报价,但检查的时候一打后,终于我没有看到它的罪魁祸首。 但是,如果这不是问题,那么我不知道那是什么? 你知道,我只是一个初学者库MySQLi。 因此,如果任何人谁拥有它更广泛的知识,并给我一些忠告,将不胜感激。
(电流)问题2:
经过长期的一系列调试中, mysqli multiple insert
最后的工作原理与外键除外。 当我检查表contact
是旁边的employees
表列eid
成功记录的准确值eid
从employees
表,但表显示0其余eid
列。 谁知道该如何处理呢? 提前致谢。
注:Kindle的检查上面的修改后的代码看到的变化,看看这些代码具有当前问题。