PHP与库MySQLi多重插入 - 错误(PHP&MySQLi multiple insert -

2019-10-20 20:11发布

美好的一天,

因与库MySQLi多重插入困难。 我有这个分贝它由6页不同的表每个它具有eidforeign keyemployees表。 下面是表列表: employees (其中ofcourse持有primary keyeid ),再contacteducationjob_descwork_historyfamilybg

什么时遇到的问题是,当我试图从插入到其中的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成功记录的准确值eidemployees表,但表显示0其余eid列。 谁知道该如何处理呢? 提前致谢。

注:Kindle的检查上面的修改后的代码看到的变化,看看这些代码具有当前问题。

Answer 1:

您正在运行的查询的顺序并不好。 你必须在使用前做查询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);


Answer 2:

在声明一个你有一个列了:

 $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);

姓两倍值!

复职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失踪



Answer 3:

您查询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。



文章来源: PHP&MySQLi multiple insert - Error
标签: php mysqli