3
我正在開發一個博客項目,我正在使用PHP從MySQL數據庫獲取數據並將其保存到JSON文件中,然後使用AngularJS輸出數據。我知道這可能不是最有效的做法,但角有一些優勢。在我的管理儀表板中,我將帖子顯示在表格中,併爲每個帖子記錄添加一個刪除按鈕。我遇到的問題是試圖抓取帖子的ID並從數據庫中刪除記錄。如果有人可以請幫助?我會非常感謝!如何使用PHP和AngularJS從MySQL數據庫中刪除記錄?
這是我的一些代碼:
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);
}
?>
有對GET請求沒有正文內容...使用'$ id',但你真的需要驗證,或詢問嚴重的問題。更不用說mysql擴展已被棄用 – charlietfl
我確實使用mysqli,這只是一個錯字,但我仍然無法使它工作。 – sidlack