MySQL Error 1452 - Cannot add or update?
This is my database:
http://i.imgur.com/D1M4J.png
I am trying to insert data into my main linking table procedure after inserting data into all the tables around it. I have checked the data being inserted and it seems ok, I have checked my relationships as well.
$proceduredata = array
(
'patient_id' => $patientfk,
'name_id' => $procedurenamefk,
'department_id' => $departmentfk,
'dosage_id' => $dosagefk,
'edocument' => NULL, //not implemented yet
'user_id' => $this->session->userdata('userID'),
'duration' => NULL, //not implemented yet
'submitted' => date('d-m-Y H:i:s', now()),
'comment' => NULL, //to be implemented
);
$insertprocedure = $this->db->insert('procedure', $proceduredata); //inserts into 'procedure' table with $proceduredata array
$procedurefk = $this->db->insert_id(); //gets the primary key of the row inserted
The error message:
A Database Error Occurred
Error Number: 1452
Cannot add or update a child row: a foreign key constraint开发者_开发百科 fails (
midas1.procedure, CONSTRAINTbelongs to aFOREIGN KEY (department_id) REFERENCESdepartment(department_id))INSERT INTO
procedure(patient_id,name_id,department_id,dosage_id,edocument,user_id,duration,submitted,comment) VALUES (17, 7, 0, NULL, NULL, '2', NULL, '06-07-2011 13:41:01', NULL)Filename: C:\xampp\htdocs\midas\system\database\DB_driver.php
Line Number: 330
Thanks for your time
$departmentfk seems to be 0, but needs to be an ID from the department table, according to your constraints in MySQL. MySQL checks the department table and doesn't find a corresponding ID, thats why it fails.
加载中,请稍侯......
精彩评论