MySQLi没有在这个程序中更新如何解决这个错误

时间:2016-02-07 09:06:05

标签: php html mysqli

此代码一个字段问题。这个文件在int中,但值在varchar中。所以如何更改连接查询。

  <?php 
include 'dblayer.php';
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update")
{ 
    $query = "update employee
    INNER JOIN department ON employee.department=department.dept_id SET
    fname = '".$mysqli->real_escape_string($_POST['fname'])."',
    lname  = '".$mysqli->real_escape_string($_POST['lname'])."',
    dept  = '".$mysqli->real_escape_string($_POST['department'])."',
    where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
    if( $mysqli->query($query) )
     {
        echo "<script>alert('employee Data updated successfully!')</script>";
        echo "<script>window.open('view.php')</script>";
    }
    else
    {
        echo "Database Error: Unable to update record.";
    }
    }
    $query = "SELECT  employee.id, employee.fname, employee.lname,    department.department from employee INNER JOIN department  ON  employee.department = department.dept_id where employee.id='".$mysqli-    >real_escape_string($_REQUEST['id'])."' limit 0,1";
    $result = $mysqli->query($query );
    $row = $result->fetch_assoc();
    $fname = $row['fname'];
    $lname = $row['lname'];
    $dept = $row['department'];
   ?>

部门字段主键和员工表ID主键

1 个答案:

答案 0 :(得分:0)

我不会将你的问题解释为100%,但我会尽力帮助你。

首先 - 表格的id是主要的和自动增量,因此更新此行没有任何意义

其次要在INNER JOIN中比较这两行,它们的数据库行必须具有相同的INT属性

employee.department=department.dept_id

在此行employee.department中,来自department.dept_id ???

的ID

第三步将代码更改为

 where employee.id