用php更新2 mysql表

voase2hg  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(269)

我想用php中的一个查询更新2个mysql表。我有三张table。一个雇员、一个公司和一个雇员\公司,其中有另外两个表的id。这就是它的样子:模态
员工姓名和地址在一个表中。如果我改变了这些,然后我点击更新按钮,它是插入更新的员工,而不是更新它。如果我从选项值(这个值是公司id)更改公司,它不会更新它,只更新员工姓名和地址。fetch.php是我将当前数据从表打印到edit模式的地方。
我怎样才能解决这个问题?

索引.php

<input type="button" name="edit" value="Edit" id="<?php echo $row["employee_id"]; ?>" class="btn btn-info btn-xs edit_data" />

    <form method="post" id="insert_form">  
                              <label>Enter Employee Name</label>  
                              <input type="text" name="name" id="name" class="form-control" />  
                              <br />  
                              <label>Enter Employee Address</label>  
                              <textarea name="address" id="address" class="form-control"></textarea>  
                              <br />  
                              <label>Company</label>
                              <select name="company" id="company" class="form-control">
                                  <?php 
                                    $query2 = "SELECT * FROM company GROUP BY company_id";  
                                    $result2 = mysqli_query($connect, $query2);  
                                    while($row2= mysqli_fetch_array($result2)){
                                ?>
                                    <option name="option" value="<?php echo $row2['company_id'];?>"><?php echo $row2['company_name'];?></option>
                                  <?php
                                }
                                ?>
                              </select>
                              <br/>
                              <input type="hidden" name="employee_id" id="employee_id" />  
                              <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />  
                         </form>  

    <script>
       $(document).on('click', '.edit_data', function(){  
               var employee_id = $(this).attr("id");  
               $.ajax({  
                    url:"fetch.php",  
                    method:"POST",  
                    data:{employee_id:employee_id},  
                    dataType:"json",  
                    success:function(data){  
                         $('#name').val(data.name);   
                         $('#address').val(data.address);
                         $('#company').val(data.company);
                         $('#employee_id').val(data.id);  
                         $('#insert').val("Update");  
                         $('#add_data_Modal').modal('show');  
                    }  
               });  
          });  
          $('#insert_form').on("submit", function(){  

               if($('#name').val() === "")  
               {  
                    alert("Name is required");  
               }  

               else  
               {  
                    $.ajax({  
                         url:"insert.php",  
                         method:"POST",  
                         data:$('#insert_form').serialize(),  
                         beforeSend:function(){  
                              $('#insert').val("Inserting");  
                         },  
                         success:function(data){  
                              $('#insert_form')[0].reset();  
                              $('#add_data_Modal').modal('hide');  
                              $('#employee_table').html(data);  
                         }  
                    });  
               }  
          });  
    </script>

获取.php

if(isset($_POST["employee_id"]))  
 {  
      $query = "SELECT employee.name, employee.address, company.company_id AS company
            FROM employee 
            LEFT JOIN employee_company ON employee.employee_id = employee_company.employee_id
            LEFT JOIN company ON company.company_id = employee_company.company_id
            WHERE employee.employee_id = '".$_POST["employee_id"]."'";  
      $result = mysqli_query($connect, $query);  
      $row = mysqli_fetch_array($result);  
      echo json_encode($row);  
 }

插入.php

$output = '';  
      $message = '';  
      $name = mysqli_real_escape_string($connect, $_POST["name"]);  
      $address = mysqli_real_escape_string($connect, $_POST["address"]); 
      $company = mysqli_real_escape_string($connect, $_POST["company"]);
      $id=$_POST["employee_id"];

      if($id != '')  
      {  
           $query = "  
           UPDATE employee, employee_company
           SET employee.name='$name',
           employee.address='$address',
           employee_company.employee_id='$id',
           employee_company.company_id='$company'
           WHERE employee.employee_id='$id'";  
           $message = 'Data Updated';

      }  
      else  
      {  
           $query = "  
           INSERT INTO employee(name, address)  
           VALUES('$name', '$address');  
           ";  
           $message = 'Data Inserted';

      }  
      if(mysqli_query($connect, $query))  
      {  
           $output .= '<label class="text-success">' . $message . '</label>';  

      }  
      echo $output;
q9rjltbz

q9rjltbz1#

请尝试此代码。

$(document).on('click', '.edit_data', function(){  
      var employee_id = $(this).attr("employee_id");//change id to employee_id 
      ////your code
)};
f87krz0w

f87krz0w2#

你不会得到 employee_id 更新时。
只是过去 '' 在传球时打到 AJAX employee_id 那也许会有帮助。

$.ajax({  
      url:"fetch.php",  
      method:"POST",  
      data:{'employee_id':employee_id},  // Make quote to pass the parameter .
      dataType:"json",

相关问题