我正在用 angular 和 php 构建一个项目。我的数据库中有一个"file"表,我可以发送文件并检索我需要的所有信息。我添加了一个删除按钮,但我不知道为什么它不起作用。我的控制台没有错误。有人可以看看我的代码吗?
用于删除的 php:
<?php
header('Content-Type: text/html; charset=utf-8');
$connect = mysqli_connect("localhost", "root", "", "hamatkin");
include_once 'file.php';
mysqli_query($connect, "SET character_set_client = utf8");
mysqli_query($connect, "SET character_set_connection = utf8");
mysqli_query($connect, "SET character_set_results = utf8");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//$customer = new Customer();
$data = json_decode(file_get_contents("php://input"));
$x = $data->reffer_customer_id;
$reffer_customer_id = $data->reffer_customer_id;
$del = "DELETE FROM file WHERE reffer_customer_id = " . $reffer_customer_id;
//echo $del;
mysqli_query($connect, $del);
$newURL = "/hamatkin/#/allPriceOffers";
header('Location: '.$newURL);
?>
Controller :
$scope.delete = function(deletingId, $index) {
$http.post('api/customers-tab/delete-priceOffer.php', { "reffer_customer_id" : deletingId })
.success(function(data) {
var arr = JSON.parse(JSON.stringify(data));
$scope.files = arr;
var arr2 = arr.split(",");
arr2.splice($index, 1);
$route.reload();
});
};
HTML 删除按钮:
<td><a ng-click="delete(x.reffer_customer_id, $index)" class="btn btn-primary btn-active">מחיקה</td>
最佳答案
首先你应该修复你的 HTML:
- 我不知道你在
ng-click
上添加删除功能是什么意思属性,可能您想使用on-click
反而?如果我错了请纠正我 - 您打开了
<a>
标签,但是</a>
不在
在 <td>
中更正了 HTML 按钮是:
<td><a on-click="delete(x.reffer_customer_id, $index); return false;" class="btn btn-primary btn-active">מחיקה</a></td>
第二,我建议检查你的表 file
的 MySQL 方案并确保您提供相同类型的 refferal_customer_id
.
例如,我想知道这应该是数值,而不是:
<?php
header('Content-Type: text/html; charset=utf-8');
/*
* I propose to check all data before you will open connection to MySQL,
* because if data is not correct - connection will not be used
* I strongly propose to process errors in your client scripts
*/
$data = json_decode(file_get_contents("php://input"));
if (!isset($data) ||
!isset($data->reffer_customer_id) ||
!is_numeric($data->reffer_customer_id)
) {
echo 'Incorrect data specified';
exit;
}
/*
* Connecting
*/
$connect = mysqli_connect("localhost", "root", "", "hamatkin");
/*
* I don't know why you using it for delete query if your id is numeric
*/
mysqli_query($connect, "SET character_set_client = utf8");
mysqli_query($connect, "SET character_set_connection = utf8");
mysqli_query($connect, "SET character_set_results = utf8");
/*
* I don't recommend you to pass mysql connection error in raw format,
* because it can be used against you
* And don't forget to halt your script if error occurred
*/
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL";
exit;
}
/*
* Here your delete query
*/
$delId = mysqli_real_escape_string($connect, $data->reffer_customer_id);
if (!$delId) {
echo 'Incorrect id for delete query specified';
exit;
}
/*
* Don't forget to check errors
*/
if (!mysqli_query($connect, "DELETE FROM file WHERE reffer_customer_id = $delId")) {
echo "Failed to delete reffer_customer with id: $delId";
exit;
}
/*
* And close the connection
*/
mysqli_close($connect);
/*
* As for me: better to put next route into response for redirecting from client
* but I don't know what you will do with this, so, putting header
*/
header('Location: /hamatkin/#/allPriceOffers');
关于php - 从数据库中删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849060/