0

Hye everyone please help me,I have a problem with my insert value which is i cannot insert more than one data.but when I look at the data value in the table,it seen like N5,N7 and N9 are missing. The error shown

"N4";"farah bt muhammad";4;"PEREMPUAN";"C5" "N6";"Maisarah";2;"PEREMPUAN";"C2" "N8";"haikal";4;"LELAKI";"C2"

Warning : pg_query():Query failed: ERROR: insert or update on table "nominee" violets foreign key constraint "custFK" DETAIL: Key(cust_id)=(C2) is not present in table "customer".

<?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); 
?>  
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
SySyBy
  • 827
  • 1
  • 7
  • 11
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Nov 28 '16 at 15:31
  • @Jay Blanchard the topic starter is using postgresql instead of mysql – Raymond Nijland Nov 28 '16 at 15:34
  • It doesn't matter @RaymondNijland, the concepts are still the same. – Jay Blanchard Nov 28 '16 at 15:35
  • Tq for the comments,but what should I do to solve the problem? @JayBlanchard – SySyBy Nov 28 '16 at 16:09

1 Answers1

2

A foreign key is a database constraint that seeks to maintain "order" as deemed by the database creator. In this case, you cannot have a "nominee" who is not in the customer table.

Either be certain you have the proper id for the nominee from the user table, or if it is a new user, insert the customer first, then get the customer's id and use it to insert the nominee.

WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • do you mean I have to insert to the customer (parent table) after inserting it into the nominee? if so, I have insert into a customer before insert to nominee. – SySyBy Nov 28 '16 at 16:14
  • i mean you cannot insert to the nominee table a customer that is not in the customer table. add customer. get custId. add nominee (with that custId). – WEBjuju Nov 28 '16 at 16:44
  • you could, of course, drop the foreign key - but you must first ask yourself - **why was this foreign key "constraint" put on the table in the first place?** – WEBjuju Nov 28 '16 at 16:46