Reputation: 56
Error: Cannot add or update a child row: a foreign key constraint fails (crimereportingsystem
.suspect
, CONSTRAINT j_fk
FOREIGN KEY (jail_id
) REFERENCES jail
(jail_id
) ON DELETE CASCADE ON UPDATE CASCADE)
$sql="INSERT INTO `suspect` (suspect_id, suspect_name, address, height, eye_colour, weight, plead_type, hair_colour, blood_group, gender, dob, jail_id, release_date, admission_date, lawyer_id, hire_type, Picture, fingerprint)
VALUES
('$suspect_id','$suspect_name','$address','$height','$eye_colour','$weight','$plead_type','$hair_colour', '$blood_group', '$gender', '$dob', '$jail_id', '$release', '$admission', '$lawyer_id', '$hire_type', '$picture', '$fingerprint') ";
Why does PHP throw the above error? What can i do?
Upvotes: 1
Views: 38
Reputation: 2168
It seems that you have wrapped too many variables with apostrophes. Try to wrap only string-valued ones, and not numeric ones. I mean something like this:
$sql="INSERT INTO suspect (suspect_id, suspect_name, ...)
VALUES
($suspect_id, '$suspect_name', ...)";
Note that $suspect_id is not wrapped (I supposes it is an integer number), while $suspect_name is wrapped (I suppose it is a string).
Apply this idea to all variables. I hope this will work.
Upvotes: 1