PHP Ajax通过Bootstrap模式更新MySQL数据

时间:2018-11-02 22:03:57

标签: php mysql ajax

我想使用php ajax模态更新我的数据库。更新相关字段时{{('#main_id')。val(data.id); } 不要将ID传递给更新查询,因此会在不执行更新查询的情况下发生插入。 console log details when trying to update          我试图解决这个问题,但是我做不到。在不更新现有数据的情况下,它会插入新记录

这是我的index.php

<?php  
$connect = mysqli_connect("localhost", "root", "", "ohmsp");  
$query = "SELECT * FROM tbl_main_category ORDER BY cat_Id DESC";  
$result = mysqli_query($connect, $query);  
?>  

  <div id="employee_table">  
    <table class="table table-bordered">  
     <tr> <th width="70%">Employee id</th> 
      <th width="70%">Employee Name</th>  
      <th width="15%">Edit</th>  
      <th width="15%">View</th>  
    </tr>  
    <?php  
    while($row = mysqli_fetch_array($result))  
    {  
     ?>  
     <tr> <td><?php echo $row["cat_Id"]; ?></td> 
      <td><?php echo $row["category_name"]; ?></td>  
      <td><input type="button" name="edit" value="Edit" id="<?php echo $row["cat_Id"]; ?>" class="btn btn-info btn-xs edit_data" /></td>  
      <td><input type="button" name="view" value="view" id="<?php echo $row["cat_Id"]; ?>" class="btn btn-info btn-xs view_data" /></td>  
    </tr>  
    <?php  
  }  
  ?>  
</table>  
</div>  
</div>  
</div>  
</body>  
</html>  
<div id="dataModal" class="modal fade">  
  <div class="modal-dialog">  
   <div class="modal-content">  
    <div class="modal-header">  
     <button type="button" class="close" data-dismiss="modal">&times;</button>  
     <h4 class="modal-title">Employee Details</h4>  
   </div>  
   <div class="modal-body" id="employee_detail">  
   </div>  
   <div class="modal-footer">  
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
   </div>  
 </div>  
</div>  
</div>  
<div id="add_data_Modal" class="modal fade">  
  <div class="modal-dialog">  
   <div class="modal-content">  
    <div class="modal-header">  
     <button type="button" class="close" data-dismiss="modal">&times;</button>  
     <h4 class="modal-title">PHP Ajax Update MySQL Data Through Bootstrap Modal</h4>  
   </div>  
   <div class="modal-body">  
     <form method="post" id="insert_form">  
      <label>Enter Employee Name</label>  
      <input type="text" name="category_name" id="category_name" class="form-control" />  
      <br />  

      <input type="hidden" name="main_id" id="main_id" />  
      <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />  
    </form>  
  </div>  
  <div class="modal-footer">  
   <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
 </div>  
</div>  
</div>  
</div>  
<script>  
 $(document).ready(function(){  
  $('#add').click(function(){  
   $('#insert').val("Insert");  
   $('#insert_form')[0].reset();  
 });  
  $(document).on('click', '.edit_data', function(){
    console.log('test')       
    var main_id = $(this).attr("id");
    console.log(main_id)    
    $.ajax({  
      url:"fetch.php",  
      method:"POST",  
      data:{main_id:main_id},  
      dataType:"json",  
      success:function(data){ 
        console.log(data)
        $('#category_name').val(data.category_name);  
        console.log(data.category_name)             
        $('#main_id').val(data.id); 
        console.log(data.id)                     
        $('#insert').val("Update");  
        $('#add_data_Modal').modal('show');  
      }  
    });  
  });  

insert.php

    <?php  
 $connect = mysqli_connect("localhost", "root", "", "ohmsp");  
 if(!empty($_POST))  
 {  
      $output = '';  
      $message = '';  
      $category_name = mysqli_real_escape_string($connect, $_POST["category_name"]);  

      if($_POST["main_id"] != '')  
      {  
           $query = "  
           UPDATE tbl_main_category   
           SET category_name='$category_name' WHERE cat_Id='".$_POST["main_id"]."'";  
           $message = 'Data Updated';  
      }  
      else  
      {  
           $query = "  
           INSERT INTO tbl_main_category(category_name)  
           VALUES('$category_name');  
           ";  
           $message = 'Data Inserted';  
      }  

fetch.php

  <?php  
 //fetch.php  
 $connect = mysqli_connect("localhost", "root", "", "ohmsp");  
 if(isset($_POST["main_id"]))  
 {  
      $query = "SELECT * FROM tbl_main_category WHERE cat_Id = '".$_POST["main_id"]."'";  
      $result = mysqli_query($connect, $query);  
      $row = mysqli_fetch_array($result);  
      echo json_encode($row);  
 }  
 ?>

1 个答案:

答案 0 :(得分:0)

您已从$('#main_id')。val(data.id)中获取了值,而不是从$('#main_id')。val(data.cat_Id)中获取了值;

您获取了id而不是cat_Id的值