用PHP更新2个MySQL表

时间:2018-06-28 09:09:05

标签: javascript php mysql

我想用一个PHP查询更新2个MySQL表。我有3张桌子。一个雇员,一个公司和一个employee_company,后者具有其他两个表的ID。外观如下:modal

员工姓名和地址在一张表中。如果更改了这些,则单击“更新”按钮,它将插入已更新的员工,而不是对其进行更新。而且,如果我将选项值(此值是公司ID)更改为公司,则它不会更新它,而只是更新员工姓名和地址。在fetch.php中,我将表中的当前数据打印到编辑模式中。

我该如何解决这个问题?

index.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>

fetch.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);  
 }  

insert.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; 

2 个答案:

答案 0 :(得分:0)

更新时没有得到employee_id

只需将''传递给ajax,同时传递employee_id可能会有所帮助。

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

答案 1 :(得分:0)

尝试使用此代码的朋友。

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