插入查询工作,更新不工作?

时间:2016-02-08 11:18:59

标签: php html5 mysqli

这个插入程序工作完美 但更新不工作任何正文帮助提供正确的代码

Insert.php

 <html>
 <form role="form" action="" method="post">
 <input type="text" name="fname" placeholder="First name..." id="fname">
 <input list="dept" placeholder="Choose Dept"  name="dept" required/>
 <datalist id="dept">
 <option>
 <?php 
  include 'dblayer.php';
  $query = mysqli_query($mysqli,"SELECT department FROM department");
  while($row=mysqli_fetch_array($query))
    {
       echo "<option value='". $row['department']."'>".$row['department'] .'</option>';
    }
  ?>
  </option>
  </datalist>

<?php
include "dblayer.php";
if(isset($_POST["submit"]))
{
    $fname  = $_POST["fname"];
    $dept   = $_POST["dept"];
    $result = mysqli_query($mysqli, "INSERT INTO employee(fname,department)
            SELECT '$fname', dept_id  FROM department WHERE department = '$dept' LIMIT 1");

    if($result)
        {
            echo "<script>alert('New employee register successfully!')</script>";
            echo "<script>window.open('home.php','_self')</script>";
        }
    else 
        {
            echo "<script>alert('something went wrong!')</script>";
        }
    }
?>

Update.php

此页面加入查询一些问题。我认为部门价值得到但不是保存记录。所以加入查询正确的格式答案给任何人

 <?php
 include 'dblayer.php';
 $action = isset( $_POST['action'] ) ? $_POST['action'] : "";
 if($action == "update")
  { 
    $query = "UPDATE employee SET fname = '".$mysqli->real_escape_string($_POST['fname'])."', department='".$mysqli->real_escape_string($_POST['department'])."' where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
   if( $mysqli->query($query) ) {
        echo "<script>alert('updated!')</script>";
        echo "<script>window.open('insert.php')</script>";}
  else{
       echo "Unable to update record.";
       }}
    $query = "SELECT  employee.fname, 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'];
    $dept = $row['department'];
?>

1 个答案:

答案 0 :(得分:1)

更改

<input list="dept" placeholder="Choose Dept"  name="dept" required/>

<input list="dept" placeholder="Choose Dept"  name="department" required/>

在您的插入脚本中:$dept = $_POST["department"];