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);
}
?>
$id
but you really need to validate it or asking for serious problems. Not to mention that mysql extension is deprecated