2014-08-30 42 views
0

好日子,PHP&MySQLi多重插入 - 錯誤

在MySQLi中多重插入有困難。我有這個數據庫,它由6個不同的表格組成,每個表格都有eid作爲foreign keyemployees表格。下面是表列表:employees(這ofcourse持有primary keyeid),然後contacteducationjob_descwork_historyfamilybg

什麼時遇到的麻煩是,當我試圖插入從形式採取了數據的索引頁面指向insert.php,其中MySQLi函數已被調用。 你可以看到下面的代碼:

[注:以下屬性僅僅是虛設。]

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表旁邊eideid成功記錄從employeeseid的確切值,但表中的其餘部分顯示0爲eid列。任何人知道如何處理它?提前致謝。

注意:Kindle會檢查上面修改的代碼以查看更改並查看代碼當前的問題。

+0

您應該使用準備好的語句。你能找出哪個語句給出了錯誤? – Jens 2014-08-30 07:28:47

+0

@Jens現在有問題了'mysqli_insert_id();'它似乎沒有從eid的原始表中獲取值。這就是爲什麼除了「僱員」表之外沒有別的東西可以顯示。即使我這樣做了'mysqli_insert_id($ db);' 現在該怎麼辦? – Archangel08 2014-09-02 02:17:05

回答

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); 
+0

謝謝@matei,我也提供了你的建議,但由於某種原因,我仍然收到了同樣的錯誤。 :( – Archangel08 2014-08-30 08:04:54

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

姓兩次值!

復職4

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

是失蹤

+0

謝謝@Jens,我刪除了語句1中的重複列,並將缺少的部分放在了語句4中的作業desc中 但是仍然存在相同的錯誤,它會在回滾後顯示錯誤消息 – Archangel08 2014-08-30 08:04:20

+0

請顯示最重要的查詢出錯? – Jens 2014-08-30 08:06:09

+0

並在執行查詢後添加'$ mysqli-> error()'以便您可以查看原因。 – Jens 2014-08-30 08:07:46

0

您查詢job_desc表錯列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賦值的值。