删除不适用于数据库

时间:2021-10-29 07:19:27

I'm building a project with angular and php. I have a "file" table in my database that I can send file and retrieve all information that I need. I added a delete button but I don't know why it doesn't work. There are no errors in my console. Can someone please have a look at my code?

我正在用angular和php构建一个项目。我的数据库中有一个“文件”表,我可以发送文件并检索我需要的所有信息。我添加了一个删除按钮,但我不知道为什么它不起作用。我的控制台没有错误。有人可以看看我的代码吗?

php for deleteing:

用于删除的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 delete button:

Html删除按钮:

<td><a ng-click="delete(x.reffer_customer_id, $index)" class="btn btn-primary btn-active">מחיקה</td>

2 个解决方案

#1


0  

First of all you should fix your HTML:

首先,你应该修复你的HTML:

  1. I don't know what you mean putting delete function on ng-click attribute, probably you want to use on-click instead? Correct me if I'm wrong
  2. 我不知道你的意思是把删除功能放在ng-click属性上,可能你想用on-click代替?如我错了请纠正我
  3. You have opened <a> tag, but </a> is absent
  4. 您已打开标记,但不存在

Corrected HTML button in <td> is:

中更正的HTML按钮是:

<td><a on-click="delete(x.reffer_customer_id, $index); return false;" class="btn btn-primary btn-active">מחיקה</a></td>

In the second I propose to check your MySQL scheme of table file and be sure that you provide same type of refferal_customer_id. For example, I've wondered that this should be numeric value, than:

在第二个我建议检查你的表格文件的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');

#2


-2  

I think your query should be like these:

我认为您的查询应该是这样的:

$del = "DELETE FROM file WHERE reffer_customer_id=".$reffer_customer_id." ";

#1


0  

First of all you should fix your HTML:

首先,你应该修复你的HTML:

  1. I don't know what you mean putting delete function on ng-click attribute, probably you want to use on-click instead? Correct me if I'm wrong
  2. 我不知道你的意思是把删除功能放在ng-click属性上,可能你想用on-click代替?如我错了请纠正我
  3. You have opened <a> tag, but </a> is absent
  4. 您已打开标记,但不存在

Corrected HTML button in <td> is:

中更正的HTML按钮是:

<td><a on-click="delete(x.reffer_customer_id, $index); return false;" class="btn btn-primary btn-active">מחיקה</a></td>

In the second I propose to check your MySQL scheme of table file and be sure that you provide same type of refferal_customer_id. For example, I've wondered that this should be numeric value, than:

在第二个我建议检查你的表格文件的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');

#2


-2  

I think your query should be like these:

我认为您的查询应该是这样的:

$del = "DELETE FROM file WHERE reffer_customer_id=".$reffer_customer_id." ";