【发布时间】:2015-08-26 12:13:29
【问题描述】:
我有一个 Angular + PHP 应用程序,但在删除客户时遇到问题。
我没有收到任何错误,实际上,应用程序返回成功,但它并没有从我的数据库中删除客户。
这是我的角度控制器
app.controller('consultar_cliente_controller', function($scope, $http){
$scope.listaDeCliente = [];
var init = function(){
$scope.buscar();
};
$scope.buscar = function(){
$http.get('consultar_cliente.php')
.success(function(data){
$scope.listaDeCliente = data;
})
.error(function(){
console.error('Erro ao executar o GET do cliente');
});
};
$scope.deletar = function(id){
$http.delete('remover_cliente.php/' + id)
.success(function(){
console.log('Cliente removido com sucesso!');
$scope.buscar();
})
.error(function(){
console.error('Erro ao remover cliente');
})
};
init();
});
这是我的 PHP
<?php
$dbh = new PDO('pgsql:host=localhost;dbname=livraria_glp', 'postgres');
/*
* Recuperando todos os detalhes da requisição HTTP do Angular
*/
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$id = $request->id;
echo $id;
$dbh->exec("DELETE FROM PRODUTO WHERE ID = '$id'") or die( $dbh->errorInfo() );
?>
这是我的 HTML
<div class="well">
<div class="container">
<h2>Dados</h2>
<div class="form-group">
<label>Nome</label>
<input class="form-control" type="text" ng-model="filtroNome" />
</div>
</div>
<div class="container resultado">
<table class="table">
<thead>
<tr>
<th>Nome</th>
<th>CPF</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="cliente in listaDeCliente | filter : filtroNome">
<td>{{cliente.nome}}</td>
<td>{{cliente.cpf}}</td>
<td>{{cliente.id}}</td>
<td><button class="btn btn-danger" ng-click="deletar(cliente.id)">Deletar</button></td>
</tr>
</tbody>
</table>
</div>
<button class="btn btn-success" onclick="window.location.href='javascript:window.history.go(-1)'">Voltar</button>
</div>
编辑:我在 PHP 上试过这个:
echo "DELETE FROM PRODUTO WHERE ID = '$id'";
它返回了:
从 ID = '' 的产品中删除
为什么 PHP 收不到 id?
【问题讨论】:
-
你好SQL注入向量,再见
PRODUTO表数据 -
菲尔,这只是一个学习项目。
-
尝试调试
$postdata。一个合适的调试器会很好,但至少,试试$postdata = file_get_contents('php://input'); var_dump($postdata); exit;
标签: php html ajax angularjs database