用php更新mysql

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

我想用php更新mysql数据库。我的问题是我有三张table。一个雇员、一个公司和一个雇员\公司(它有另外两个表的id)。如果我更改雇员的姓名,我想更新employee表,如果我向雇员添加公司,我想更新employee\u company表。我试图解决这个问题,但它没有更新表。我该怎么做?

索引.php

<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 ORDER BY company_id";  
                                $result2 = mysqli_query($connect, $query2);  
                                while($row2= mysqli_fetch_array($result2)){
                            ?>
                                <option value="<?php echo $row2['company_id'];?>"><?php echo $row2['company'];?></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(){  

           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

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

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

ryevplcw1#

查询中有一些错误,使用了附加的 comma(,) 在何处条件之前。所以把它去掉。检查以下更新的代码:

if ($_POST["employee_id"] != '') {
    $query = "UPDATE employee, employee_company SET employee_company.employee_id='$id', employee_company.company_id='$company', employee.name='$name' WHERE employee.employee_id='" . $_POST["employee_id"] . "'";
    mysqli_query($connect,$query);
    $message = 'Data Updated';
}

注意:我还建议您在查询中使用之前清除所有输入。

stszievb

stszievb2#

你失踪了 mysqli_query() 实际运行查询的函数。你的 $query 变量只是一个字符串变量,除非您将其放入 mysqli_query() 功能
用这段代码替换你的代码

if($_POST["employee_id"] != '')  
  {  
       $query = "  
       UPDATE employee, employee_company 
       SET employee_company.employee_id='$id',
       employee_company.company_id='$company',
       employee.name='$name'
       WHERE employee.employee_id='".$_POST["employee_id"]."'";  
       if(mysqli_query($connect,$query))
       $message = 'Data Updated';
       else
       echo mysqli_error($connect);  
  }

如果有任何疑问,那么你可以问我自由!

相关问题