无法插入多个数据

时间:2016-11-28 15:27:48

标签: php postgresql

Hye大家请帮助我,我的插入值有问题,即我无法插入多个数据。但是当我查看表中的数据值时,它看起来像N5,N7和N9都缺失了。显示的错误

“N4”;“farah bt muhammad”; 4;“PEREMPUAN”;“C5” “N6”; “Maisarah”; 2; “PEREMPUAN”; “C2” “N8”; “海卡尔”; 4; “LELAKI”; “C2”

  

警告:pg_query():查询失败:错误:在表“nominee”上插入或更新紫罗兰外键约束“custFK”   详细信息:表“customer”中不存在键(cust_id)=(C2)。

<?php  
$connection = pg_connect ("user = postgres password = syafiqah26 port = 5433  dbname = bengkel2 host = localhost");
$number = count($_POST["name"]);  
$number1 = count($_POST["gender"]);
$number2 = count($_POST["age"]);
$number3 = count($_POST["hidden"]);

if(($number > 0)&&($number1>0)&&($number2>0)&&($number3>0))  
{  
  for($i=0,$j=0,$k=0,$l=0; $i<$number && $j<$number1 && $k<$number2 &&    $l<$number3; $i++,$j++,$k++,$l++)  
  {  
       if((trim($_POST["name"][$i] != ''))&&(trim($_POST["gender"][$j] !=  ''))&&(trim($_POST["age"][$k] != ''))&&(trim($_POST["hidden"][$l] !='')))  
       {  
            $sql = "INSERT INTO nominee(name,gender,age,cust_Id) VALUES('".pg_escape_string($connection, $_POST["name"][$i])."','".pg_escape_string($connection, $_POST["gender"][$j])."','".pg_escape_string($connection, $_POST["age"][$k])."','".pg_escape_string($connection, $_POST["hidden"][$l])."')";  
            pg_query($connection, $sql);  

       }  
  }  
  echo "Data Inserted";  


}  
else  
 {  
  echo "Please Enter Name";  
 } 



   pg_close($connection); 
?>  

1 个答案:

答案 0 :(得分:2)

外键是一种数据库约束,旨在维护&#34; order&#34;由数据库创建者认为。在这种情况下,你不能拥有&#34;被提名者&#34;谁不在客户

要么确定您拥有来自用户表的被提名者的正确ID,要么是新用户,先插入客户,然后获取客户的ID并使用它来插入提名