无法更新子表PHP MySQL中的数据

时间:2013-03-21 20:05:48

标签: php mysql

我有两个数据库表。第一个存储员工信息,另一个存储工作信息。 我无法更新子表中的作业标题和说明我收到此错误:

  

“无法添加或更新子行:外键约束失败   (company_employee_datajob_info,CONSTRAINT job_info_ibfk_1   FOREIGN KEY(employee_Id)参考employee_infoemployee_Id)   ON UPDATE CASCADE)“。

以下是更新和插入代码。

        $query = "SELECT * FROM `employee_info` WHERE `name_of_employee` = '$employee_name'";
                $sqlsearch = mysql_query($query);
                $resultcount = mysql_num_rows($sqlsearch);
                $row=mysql_fetch_assoc ($sqlsearch);
                $employee_id= $row['employee_id'];
                //Search the database for duplicate data 
                    if ($resultcount > 0)
                        {
                            mysql_query(
                            "UPDATE `employee_info` SET 
                            `name_of_employee` = '$employee_name',
                            `Physical_Address` = '$physical_address',
                            `Phone_Number` = '$phone_number',
                            `Email_Address` = '$email_address'     
                            WHERE `name_of_employee` = '$employee_name'") 
                            or die(mysql_error());

                        }

                else {
                                $sql="INSERT INTO employee_info (
                                 name_of_employee,
                                Physical_Address,
                                Phone_Number,
                                Email_Address
                                )   
                                VALUES (
                                '$employee_name',
                                '$physical_address',
                                '$phone_number',
                                '$email_address'
                               )";  
                            if(!mysql_query($sql))
                            { 
                            die(mysql_error());
                            }
                        }


                //update and insert  statement for the child table

                $qry = "SELECT * FROM `job_info` WHERE `employee_id` = '$employee_id AND `job_title` = '$job_title'";
                $sqlsearcher = mysql_query($qry);
                $resultcounter = mysql_num_rows($sqlsearcher);
                //Search the database for duplicate data 
                if ($resultcounter > 0) 
                {

                    mysql_query(
                    "UPDATE `job_info` SET 
                    `job_description` = '$job_description',
                    `job_title` = '$job_title'
                    WHERE `employee_id` = '$employee_id AND `job_title` = '$job_title'") 
                    or die(mysql_error());
                }
                else
                {
                    $sql2="INSERT INTO job_info (
                        employee_id,
                        job_description,
                        job_title
                        )   
                        VALUES  (
                        '$employee_id',
                        '$job_description',
                        '$job_title'
                        )";  
                        if(!mysql_query($sql2))
                            { 
                        die(mysql_error());
                            }

                }

0 个答案:

没有答案