PHP& MySQLi多次插入 - 错误

时间:2014-08-30 07:25:10

标签: php mysqli

美好的一天,

我在使用MySQLi多次插入时遇到困难。我有这个由6个不同的表组成的数据库,每个表都有eid作为foreign key表中的employees。以下是表格列表:employees(其中包含primary key的{​​{1}}),然后是eidcontacteducationjob_descwork_history

遇到麻烦的是当我尝试将索引页面形式的数据插入到调用了MySQLi函数的insert.php时。 你可以看到下面的代码:

[注意:以下属性仅仅是假的。]

insert.php(修订版)

familybg

当我尝试运行程序时,输出显示: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); ?> 。它只暗示insert语句无法继续,因此返回mysqli_rollback(); 起初我想也许它与引号有关,但经过十几次检查后我终于认为它不是罪魁祸首。但如果那不是问题那么我想知道它是什么?你知道我只是MySQLi的初学者。因此,如果有任何人对此有更广泛的了解并会给我一些建议,我们将不胜感激。

(当前)问题#2:

经过一系列的调试后,ERROR: Could not save data!最终可以使用外键除外。当我检查mysqli multiple insert表旁边的contact表时,employees列已成功记录eid表中eid的确切值,但表格的其余部分显示为employees列的0。谁知道如何处理它?提前谢谢。

注意:Kindle检查了上面的修订代码以查看更改并查看代码所存在的当前问题。

3 个答案:

答案 0 :(得分:0)

您运行查询的顺序并不好。在使用mysqli_insert_id()函数获取最后插入的id之前,您必须执行查询。

  • $surname在第一个声明
  • 中重复

这样做:

//build sql statements
$sql1 = "INSERT INTO employees (fname,mname,
    lname,age,
    gender,birthday,
    birthplace,
    height,weight) 
        VALUES 
        ('$fname','$mname',
        '$surname','$surname',
        '$age','$gender',
        '$birth','$place',
        '$height','$weight')";

$r1 = mysqli_query($db,$sql1);
$id = mysqli_insert_id();


$sql2 = "INSERT INTO contact (eid,address,province,postcode,telno,mobile,email,alternate)
        VALUES('$id','$address','$province','$postcode','$telno','$mob','$email','$alter')";

$sql3 = "INSERT INTO education (eid,elem,egrad,high,college,cgrad) VALUES ('$id','$elem','$egrad','$high','$col','$cgrad')";

$sql4 = "INSERT INTO job_desc (eid,position,status,hireDate,department,job_desc,basic,salary) VALUES ('$id','$pose','$stat','$hstart','$dept','$pay','$salary')";

$sql5 = "INSERT INTO work_history (eid,company_name,position,desc,startDate,endDate) VALUES ('$id','$prev','$pold','$job','$sdate','$edate')";

$sql6 = "INSERT INTO familybg (eid,fatherName,motherName,sibling,spouse,children) VALUES ('$id','$dad','$mom','$kname','$spouse','$child')";


$r2 = mysqli_query($db,$sql2);
$r3 = mysqli_query($db,$sql3);
$r4 = mysqli_query($db,$sql4);
$r5 = mysqli_query($db,$sql5);
$r6 = mysqli_query($db,$sql6);

答案 1 :(得分:0)

在声明一中你有一个很多的专栏:

 $sql1 = "INSERT INTO employees (fname,mname,
        lname,age,
        gender,birthday,
        birthplace,
        height,weight) 
            VALUES 
            ('$fname','$mname',
            '$surname','$surname', 
            '$age','$gender',
            '$birth','$place',
            '$height','$weight')";
            $id= mysqli_insert_id($db);

姓氏是价值的两倍!

Instatement 4

$sql4 = "INSERT INTO job_desc (eid,position,status,hireDate,department,job_desc,basic,salary) VALUES ('$id','$pose','$stat','$hstart','$dept','$pay','$salary')";

是列job_desc缺失的值

答案 2 :(得分:0)

您查询job_desc表是错误的。

$sql4 = "INSERT INTO job_desc (eid,position,status,hireDate,department,job_desc,basic,salary)                              VALUES ('$id','$pose','$stat','$hstart','$dept','$pay','$salary')";

在值中,您忘记为job_desc赋值。