iam无法从这个php ajax代码中获取数据有什么问题吗

dgsult0t  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(251)

我是新的网络开发,这是我的问题。
现在,我的php ajax代码面临着一个问题,它应该用来获取数据。通过ajax调用,我无法从php文件中获取数据,而php文件从数据库中获取一些数据。
这是我的html代码

<div class="container" >  

   <br />
   <div align="right">
    <button type="button" name="add" id="add" class="btn btn-success">Add</button>
   </div>
   <br />
   <div id="image_data">

   </div>
  </div>  

<div id="imageModal" class="modal fade" role="dialog">
 <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">Add Image</h4>
   </div>
   <div class="modal-body">
    <form id="image_form" method="post" enctype="multipart/form-data">
     <p><label>Select Image</label>
     <input type="file" name="image" id="image" /></p><br />
     <input type="hidden" name="action" id="action" value="insert" />
     <input type="hidden" name="image_id" id="image_id" />
     <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-info" />

    </form>
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
   </div>
  </div>
 </div>
</div>
$(document).ready(function(){

     fetch_data();

     function fetch_data()
     {
      var action = "fetch";
      $.ajax({
       url:"carimglistupdate.php",
       method:"POST",
       data:{action:action},
       success:function(data)
       {
        $('#image_data').html(data);
       }
      })
     }
     $('#add').click(function(){
      $('#imageModal').modal('show');
      $('#image_form')[0].reset();
      $('.modal-title').text("Add Image");
      $('#image_id').val('');
      $('#action').val('insert');
      $('#insert').val("Insert");
     });
     $('#image_form').submit(function(event){
      event.preventDefault();
      var image_name = $('#image').val();
      if(image_name == '')
      {
       alert("Please Select Image");
       return false;
      }
      else
      {
       var extension = $('#image').val().split('.').pop().toLowerCase();
       if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
       {
        alert("Invalid Image File");
        $('#image').val('');
        return false;
       }
       else
       {
        $.ajax({
         url:"carimglistupdate.php",
         method:"POST",
         data:new FormData(this),
         contentType:false,
         processData:false,
         success:function(data)
         {
          alert(data);
          fetch_data();
          $('#image_form')[0].reset();
          $('#imageModal').modal('hide');
         }
        });
       }
      }
     });
     $(document).on('click', '.update', function(){
      $('#image_id').val($(this).attr("id"));
      $('#action').val("update");
      $('.modal-title').text("Update Image");
      $('#insert').val("Update");
      $('#imageModal').modal("show");
     });
     $(document).on('click', '.delete', function(){
      var image_id = $(this).attr("id");
      var action = "delete";
      if(confirm("Are you sure you want to remove this image from database?"))
      {
       $.ajax({
        url:"carimglistupdate.php",
        method:"POST",
        data:{image_id:image_id, action:action},
        success:function(data)
        {
         alert(data);
         fetch_data();
        }
       })
      }
      else
      {
       return false;
      }
     });
});

这是我的php代码我想从数据库获取数据,但我没有这样做,请任何帮助将不胜感激。

<?php
//action.php
if(isset($_POST["action"]))
{
 $connect = mysqli_connect("localhost", "aaaa", "aaaa", "aaaa");
 if($_POST["action"] == "fetch")
 {
  $query = "SELECT * FROM carimg ORDER BY carimgid DESC";
  $result = mysqli_query($connect, $query);
  $output = '
   <table class="table table-bordered table-striped">  
    <tr>
    <th></th>
     <th width="70%">Image</th>
     <th width="10%">Change</th>
     <th width="10%">Remove</th>
    </tr>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '

    <tr>
     <td>'.$row["carimgid"].'</td>
     <td>
      <img src="data:image/jpeg;base64,'.base64_encode($row['carimg'] ).'" height="60" width="75" class="img-thumbnail" />
     </td>
     <td><button type="button" name="update" class="btn btn-warning bt-xs update" id="'.$row["carimgid"].'">Change</button></td>
     <td><button type="button" name="delete" class="btn btn-danger bt-xs delete" id="'.$row["carimgid"].'">Remove</button></td>
    </tr>
   ';
  }
  $output .= '</table>';
  echo $output;
 }

if($_POST["action"] == "insert")
 {
  $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
  $query = "INSERT INTO tbl_images(name) VALUES ('$file')";
  if(mysqli_query($connect, $query))
  {
   echo 'Image Inserted into Database';
  }
 }

 if($_POST["action"] == "update")
 {
  $file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
  $query = "UPDATE carimg SET carimg = '$file' WHERE carimgid = '".$_POST["image_id"]."'";
  if(mysqli_query($connect, $query))
  {
   echo 'Image Updated into Database';
  }
 }
 if($_POST["action"] == "delete")
 {
  $query = "DELETE FROM carimg WHERE carimgid = '".$_POST["image_id"]."'";
  if(mysqli_query($connect, $query))
  {
   echo 'Image Deleted from Database';
  }
 }
}
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题