3

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!

This is some of my code:

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

.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
    $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);
    }
?>
2
  • There are no body contents for GET request...use $id but you really need to validate it or asking for serious problems. Not to mention that mysql extension is deprecated Commented Mar 8, 2016 at 18:46
  • I do use mysqli that was just a typo but I still can't get it to work. Commented Mar 8, 2016 at 21:44

1 Answer 1

4

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

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
  $connect = mysqli_connect('localhost', 'root', '', 'cms1');

  if(isset($_GET['id'])){
    $id = $_GET['id'];
    $del = "DELETE FROM posts WHERE id='".$id."'";
    mysql_query($connect, $del);
  }
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.