我正在使用SQLite作为本地数据库在AngularJS中构建应用程序。连接由PHP完成。我可以读取数据,我可以添加数据但是当我想删除数据时,我收到500内部错误。以下代码是我在项目中使用的代码。
app.js
$scope.remove = function() {
var removeIds = helperFactory.filterFiledArrayByDone($scope.items, 'id', 1);
if (removeIds.length > 0) {
$http({
method : 'POST',
url : urlRemove,
data : "ids=" + removeIds.join('|'),
headers : {'Content-Type' : 'application/x-www-form-urlencoded'}
})
.success(function(data){
if (_recordRemovedSuccessfully(data)){
$scope.items = $scope.items.filter(function(item) {
return item.done == 0;
});
}
})
.error(function(data, status, headers, config){
throw new Error('Something went wrong with removing')
});
}
};
remove.php
<?php
try {
if (empty($_POST['ids'])) {
throw new PDOException("Invalid Request";
}
$ids = $_POST['ids'];
$idsArray = explode('|', $ids);
$placeholders = implode(', ', array_fill(0, count($idsArray), '?'));
$objDb = new PDO('sqlite:../database/database');
$objDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM 'items'
WHERE 'id' IN ({$placeholders})";
$statement = $objDb->prepare($sql);
if (!$statement->execute($idsArray)) {
throw new PDOException("The execute method failed");
}
echo json_encode(array(
'error' => false
), JSON_HEX_TAG| JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
} catch(PDOException $e){
echo json_encode(array(
'error' => true,
'message' => $e->getMessage()
), JSON_HEX_TAG| JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
}
的index.html
<div class="columns">
<div class="flr">
<button type="button" class="small button primary" ng-click="remove()"><i class="fa"></i>Remove</button>
</div>
</div>
CONSOLE.LOG
angular.js:9866 POST http://localhost:8888/mod/remove.php 500 (Internal Server Error)
angular.js:11655 Error: Something went wrong with removing
at app.js:199
at angular.js:9415
at processQueue (angular.js:13248)
at angular.js:13264
at Scope.$get.Scope.$eval (angular.js:14466)
at Scope.$get.Scope.$digest (angular.js:14282)
at Scope.$get.Scope.$apply (angular.js:14571)
at done (angular.js:9698)
at completeRequest (angular.js:9888)
at XMLHttpRequest.requestLoaded (angular.js:9829)
有谁知道如何解决这个问题?
答案 0 :(得分:0)
也许我错了,这不是问题,但你错过了一个&#39;)&#39;在这里:
if (empty($_POST['ids'])) {
throw new PDOException("Invalid Request";
}