[英]PHP&MySQLi multiple insert - Error
Good Day, 美好的一天,
am having difficulty with MySQLi multiple insert. MySQLi多重插入有困难。 I have this db which consists of 6 different tables each of it has the
eid
as the foreign key
from employees
table. 我有这个数据库,它由6个不同的表组成,每个表都有
eid
作为来自employees
表的foreign key
。 Here are the list of the tables: employees
(which ofcourse holds the primary key
for eid
), then contact
, education
, job_desc
, work_history
, familybg
以下是这些表的列表:
employees
(当然,哪个拥有eid
的primary key
),然后contact
, education
, job_desc
, work_history
, familybg
What am having trouble with is when I tried to insert a data taken from the forms of the index page to the insert.php where the MySQLi function's been called out. 遇到麻烦的是,当我尝试将从索引页的形式获取的数据插入到调用MySQLi函数的insert.php中时。 You could see the code below:
您可以看到以下代码:
[NOTE: The following attributes are mere dummy.] [注意:以下属性仅仅是假的。]
insert.php (Revised) 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);
?>
The output shows: ERROR: Could not save data!
输出显示:
ERROR: Could not save data!
when I tried to run the program. 当我尝试运行该程序时。 It only implies that the insert statement couldn't proceed and thus returns to the mysqli_rollback();
它仅意味着insert语句无法继续执行,因此返回到mysqli_rollback();。 At first I thought perhaps it has something to do with the quotes, but after a dozen of times of checking finally I don't see it as the culprit.
起初我以为这可能与报价有关,但是经过十几次检查之后,我才不认为这是罪魁祸首。 But if that is not the issue then I wonder what was it?
但是,如果这不是问题,那我想知道是什么吗? You know am just a beginner with MySQLi.
您知道我只是MySQLi的初学者。 So if anyone who has broader knowledge about it and will give me some piece of advice will be appreciated.
因此,如果有人对此有更广泛的了解并愿意给我一些建议,将不胜感激。
(Current) Issue #2: (当前)问题2:
After a long series of debugging, the mysqli multiple insert
finally works with the exception of the foreign key. 经过一连串的调试之后,
mysqli multiple insert
终于可以工作了,但外键除外。 When I checked the table for contact
which is next to the employees
table the column for eid
successfully recorded the exact value of the eid
from employees
table, but the rest of the table displays 0 for eid
column. 当我检查表
contact
是旁边的employees
表列eid
成功记录的准确值eid
从employees
表,但表显示0其余eid
列。 Anyone who knows how to deal with it? 有人知道如何处理吗? Thanks in advance.
提前致谢。
NOTE: Kindle checked the revised code above to see the changes and to see the current issue the codes are having. 注意:Kindle检查了上面的修订代码,以查看更改并查看代码当前存在的问题。
The order you are running the queries is not good. 您正在运行查询的顺序不好。 You have to do the query before using
mysqli_insert_id()
function to get the last inserted id. 在使用
mysqli_insert_id()
函数获取最后插入的ID之前,必须先进行查询。
$surname
is duplicated in first statement $surname
在第一条语句中重复 Do it like this: 像这样做:
//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);
In statement one you have an column to much: 在陈述一中,您有一个专栏:
$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);
surname is twice in values! 姓氏是值的两倍!
Instatement 4 陈述书4
$sql4 = "INSERT INTO job_desc (eid,position,status,hireDate,department,job_desc,basic,salary) VALUES ('$id','$pose','$stat','$hstart','$dept','$pay','$salary')";
is the value for column job_desc
missing 缺少列
job_desc
的值
You query for job_desc table is wrong. 您查询的job_desc表是错误的。
$sql4 = "INSERT INTO job_desc (eid,position,status,hireDate,department,job_desc,basic,salary) VALUES ('$id','$pose','$stat','$hstart','$dept','$pay','$salary')";
In Values you forgot to give value for job_desc. 在“值”中,您忘记为job_desc提供值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.