Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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);
    }
?>
share|improve this question
    
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 – charlietfl Mar 8 at 18:46
    
I do use mysqli that was just a typo but I still can't get it to work. – sidlack Mar 8 at 21:44

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);
  }
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.