Reputation: 957
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);
?>
Upvotes: 0
Views: 149
Reputation: 6591
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.
Upvotes: 2