0

我在数据库中有两个表,Employee_info(stores all employee information )并且Job_Info(store all job title , job description)。代码插入数据没有问题,但是更新时在浏览器上打印此错误:

"Cannot add or update a child row: a foreign key constraint fails (Employee_database.job_info, CONSTRAINT job_info_ibfk_4 FOREIGN KEY (Employee_Id) REFERENCES Employee_info (Employee_Id) ON UPDATE CASCADE)".

可能是什么问题呢 ?这是代码

    $query = "SELECT * FROM `Employee_info` WHERE `Name_Of_Employee` = '$Name'";
      $sqlsearch = mysql_query($query);
         $resultcount = mysql_numrows($sqlsearch);
   if ($resultcount > 0) 
 {
  mysql_query("UPDATE  `employee_info` SET `Name_Of_Employee` = '$Name',

                                         `Physical_Address` = '$P_Address',

                                         `Phone_Number` = '$Phone',

                                         `Email_Address` = '$E_Address'
             WHERE `Name_Of_Employee` = '$Name'"
           )
    or die(mysql_error());
    }
                  else {
                          $sql="INSERT INTO employee_info
                                 (
                             Name_Of_Employee,
                             Physical_Address,
                             Phone_Number,
                             Email_Address
                         )
                     VALUES (
                        '$Name',
                        '$P_Address',
                        '$Phone',
                        '$E_Address'
                                 );
                          if(!mysql_query($sql))
                           {    
                         die('cannot store in employee_info'.mysql_error());
                           }
}

$Employee_Id=mysql_insert_id();
 $qry = "SELECT * FROM `Job_info` WHERE `Job_Title` = '$Job_Title'";
  $sqlsearch = mysql_query($query);
       $resultcount = mysql_numrows($sqlsearch);
     if ($resultcount > 0) {
                        "UPDATE `Job_info` SET `Employee_Id` = '$Employee_Id' ,
                               `Job_Title` = '$Job_Title',
                               `Job_Description` = '$Job_Description'

                             WHERE `Employee_Id` = '$Employee_Id'")
                         or die(mysql_error());
}
else 
{
$sql="INSERT INTO ad_info (
                       Employee_Id,
                       Job_Title,
                       Job_Description
                       )
                    VALUES (
                       '$Employee_Id',
                       '$Job_Title',
                       '$Job_Description'
                       )";
                       if(!mysql_query($sql))
                       {
                       die('cannot store in job info'.mysql_error());}
                       }

这是数据库设计

+------------------+-------------+------+-----+---------+----------------+
| Field            | Type        | Null | Key | Default | Extra          |
+------------------+-------------+------+-----+---------+----------------+
| Employee_Id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| Name_Of_Employee | varchar(20) | NO   |     |         |                |
| Physical_Address | varchar(25) | NO   |     |         |                |
| Phone_Number     | int(14)     | NO   |     |         |                |
| Email_address    | varchar(25) | NO   |     |         |                |                             |
+------------------+-------------+------+-----+---------+----------------+

+------------------+-------------+------+-----+---------+----------------+
| Field            | Type        | Null | Key | Default | Extra          |
+------------------+-------------+------+-----+---------+----------------+
| jOB_Id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| Employee_Id      | int(11)     | NO   |     |         |                |
| Job_Title        | varchar(25) | NO   |     |         |                |
| Job_Description  | text(100)   | NO   |     |         |                |
+------------------+-------------+------+-----+---------+----------------+
4

2 回答 2

0

这正是你的错误告诉你的意思。

由于存在约束,您无法添加或更新子行。这意味着更新受到限制,或者密钥不存在。如果是后者,则必须首先在引用的表中创建该键。

如果您不切换到mysqli_PDO使用准备好的语句,您将容易受到 SQL 注入的攻击。

于 2013-02-06T16:18:20.127 回答
0

回应你的$sql说法。99% 肯定你会看到你的错误。您显然在尝试放入表中的内容以及它与外键约束的关系方面存在错误。

IE,您的 FK 想要一个 1-10 的数字,而您正试图输入 11。

于 2013-02-06T16:19:10.767 回答