脚本更新子表然后停止所有其他更新

时间:2013-02-13 06:39:28

标签: php mysql

我在数据库中有两个表:

  • Employee_info(存储所有员工信息)和

  • Job_Info(存储所有职位,职位描述)。

代码插入数据没有任何问题,但更新此错误时会在浏览器上打印:

  

无法添加或更新子行:外键约束失败   (Employee_database.job_info,CONSTRAINT job_info_ibfk_4 FOREIGN KEY   (Employee_Id)REFERENCES Employee_info(Employee_Id)ON UPDATE   CASCADE)

我无法获得要更新子表的Employee_Id值或可能出现的问题?

这是代码

 $query = "SELECT * FROM `Employee_info` WHERE `Name_Of_Employee` = '$Name'";
   $sqlsearch = mysql_query($query);
       $resultcount = mysql_numrows($sqlsearch);
         if ($resultcount > 0) 
            {
                 $row=mysql_fetch_assoc ($sqlsearch);
                 $Employee_Id= $row['Employee_Id'];
                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   |     |         |                |
+------------------+-------------+------+-----+---------+----------------+

2 个答案:

答案 0 :(得分:0)

问题是更新$ employee_id时为空。看到他们你没有设置它。

您的代码应为:

$query = "SELECT * FROM `Employee_info` WHERE `Name_Of_Employee` = '$Name'";
$sqlsearch = mysql_query($query);
$resultcount = mysql_numrows($sqlsearch);
if ($resultcount > 0) 
{
   $row=mysql_fetch_assoc ($sqlsearch);
   $Employee_Id= $row['Employee_Id'];
            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());}




                                             }

答案 1 :(得分:0)

是的,您需要从父表中获取员工ID才能更新子表 因为你正在使用子表的更新语句。

添加以下代码行,然后再次执行:

$query = "SELECT * FROM `Employee_info` WHERE `Name_Of_Employee` = '$Name'";
$sqlsearch = mysql_query($query);
$resultcount = mysql_numrows($sqlsearch);
if ($resultcount > 0) 
{
   $row=mysql_fetch_assoc ($sqlsearch);
   $Employee_Id= $row['Employee_Id']; // add this line in your code here 

   .....