如何使用PHP和AngularJS从MySQL数据库中删除记录?

时间:2021-06-02 01:54:05

I am working on a blog project and I am using PHP to grab data from a MySQL database and saving it into a JSON file and then outputting the data with AngularJS. I know this might not be the most efficient thing to do but angular has some advantages. In my admin dashboard I have the posts displayed in a table along with a delete button for each post record. The problem I am having is trying to grab the id of a post and deleting the record from the database. If someone could please help? I would be very thankful!

我正在开发一个博客项目,我使用PHP从MySQL数据库获取数据,并将其保存到JSON文件中,然后使用AngularJS输出数据。我知道这可能不是最有效的方法但角度有一些优势。在我的管理仪表板中,我将文章显示在一个表中,并为每个帖子记录提供一个删除按钮。我遇到的问题是试图获取post的id并从数据库中删除记录。如果有人能帮忙吗?我将非常感激!

This is some of my code:

这是我的一些代码:

HTML

HTML

<tbody ng-repeat="post in posts">
    <tr>
        <td>{{post.id}}</td>
        <td>{{post.date}}</td>
        <td><img src="../{{post.image}}" width="100px" height="70px"></td>
        <td>{{post.title}}</td>
        <td>{{post.description | limitTo:250}} .....</td>
        <td>{{post.category}}</td>
        <td>{{post.author}}</td>
        <td>{{post.status}}</td>
        <td><a ng-click="display()" class="btn btn-primary"><i class="fa fa-pencil-square-o"></i></a></td>
        <td><a ng-click="delete(post.id, $index)" class="btn btn-danger"><i class="fa fa-close"></i></a></td>
    </tr>
</tbody>

AngularJS

AngularJS

.controller('PostsCtrl', ['$scope', '$http', function($scope, $http, $timeout) {
    $http.get('../data/posts.json').success(function(data) {
        $scope.posts = data;
    });

    $scope.delete = function(deletingId, index) {
        $http.get("functions/deletePost.php?id=" + deletingId)
            .success(function(data) {
                $scope.data.splice(index, 1);

            })
    }
}])

PHP

PHP

<?php
    $connect = mysqli_connect('localhost', 'root', '', 'cms1');

   if (isset($_GET ['id'])){

        $id = $_GET ['id'];
        $data = json_decode(file_get_contents("php://input"));
        $index = $data['id'];
        $delete = "DELETE FROM posts WHERE id='$index'";

        $result = mysql_query($connect,$delete);
    }
?>

1 个解决方案

#1


4  

I don't think that the GET gets what it should. Try sending request with following code.

我不认为得到应该得到的。尝试用以下代码发送请求。

AngularJS

AngularJS

$scope.delete = function(deletingId, index) {
  var params = $.param({"id":deletingId});
  $http({
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    url: 'functions/deletePost.php',
    method: "GET",
    data: params
  }).success(function(data){
    $scope.data.splice(index, 1);
  });
}      

PHP

PHP

<?php
  $connect = mysqli_connect('localhost', 'root', '', 'cms1');

  if(isset($_GET['id'])){
    $id = $_GET['id'];
    $del = "DELETE FROM posts WHERE id='".$id."'";
    mysql_query($connect, $del);
  }
?>

#1


4  

I don't think that the GET gets what it should. Try sending request with following code.

我不认为得到应该得到的。尝试用以下代码发送请求。

AngularJS

AngularJS

$scope.delete = function(deletingId, index) {
  var params = $.param({"id":deletingId});
  $http({
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    url: 'functions/deletePost.php',
    method: "GET",
    data: params
  }).success(function(data){
    $scope.data.splice(index, 1);
  });
}      

PHP

PHP

<?php
  $connect = mysqli_connect('localhost', 'root', '', 'cms1');

  if(isset($_GET['id'])){
    $id = $_GET['id'];
    $del = "DELETE FROM posts WHERE id='".$id."'";
    mysql_query($connect, $del);
  }
?>