【问题标题】:PHP AJAX Delete Record - deletion only works 1 timePHP AJAX 删除记录 - 删除只有效 1 次
【发布时间】:2019-05-27 08:59:33
【问题描述】:

我正在使用 ajax 和 php 删除记录。当我单击按钮时,它会删除记录,但是当我单击删除另一条记录时,它什么也不做。我做错了什么?

HTML

<form id="prop_remove">
    <input type="hidden" name="id" id="last_id" value="<?php echo $id; ?>">
    <input type="hidden" name="user" id="last_user" value="<?php echo $user; ?>">
    <input type="button" name="submit" id="last_prop" class="button fullwidth margin-top-5" value="Delete">
</form>

AJAX

<script>
$(document).ready(function() {
    $('#last_prop').click(function() {
        var id = $('#last_id').val();
        var user = $('#last_user').val();
        $.ajax({
            url: "delete.php",
            method: "POST",
            data: {
                ilan_id: id,
                ilan_user: user
            },
            success: function(response) {
                if (response == 1) {
                    $('#last_prop').closest('tr').css('background', 'tomato');
                    $('#last_prop').closest('tr').fadeOut(800, function() {
                        $(this).remove();
                    });
                } else {
                    alert('Invalid id');
                }
            }
        });
    });
});
</script>

PHP

<?php
    require_once 'config.php';
    $id     =  $_POST['ilan_id'];
    $user   =  $_POST['ilan_user'];

    $checkRecord    = "SELECT * FROM last_tbl WHERE id = '$id' AND user = '$user'";
    $check_result   = mysqli_query($conn, $checkRecord);
    $totalrows      = mysqli_num_rows($check_result);

    if($totalrows > 0){
        $delete_sql     =   "DELETE FROM last_tbl WHERE id = '$id' AND user = '$user';";
        $delete_result  =   mysqli_query($conn, $delete_sql);
        echo 1;
        exit;
    }

?>

【问题讨论】:

  • 在 click 函数处添加一个 console.log 并检查它是否被第二次调用。
  • 您的代码易受 SQL 注入攻击。您应该使用准备好的语句。
  • 您到底想达到什么目的?可能有更简单的方法来做到这一点。它不起作用,因为您在删除时没有为 last_userlast_id 输入设置新值。
  • $user$id 的值是硬连线到代码中的。因此,当您再次单击该按钮时,它会尝试删除相同的记录,但失败了。
  • 如前所述,它绝对容易受到 sql 注入的影响,因为当您再次尝试删除同一条记录时,它实际上并不存在于数据库中。所以我建议你在删除记录之前检查记录是否存在。如果它确实存在,那么它的罚款。此外,您应该开始使用对 SQL 注入安全的准备好的语句(只要您不只是在其他地方做不安全的事情(即通过字符串连接构造 SQL 语句))

标签: javascript php jquery mysql ajax


【解决方案1】:

您的问题是您正在覆盖 HTML 元素 ID。您可以删除表单并改用单个按钮,并通过按钮的data 属性传递数据。

用一个按钮替换你的表单

<button class="button fullwidth margin-top-5 last_prop" data-last-id="<?= $id; ?>" data-last-user="<?= $user; ?>">Delete</button>

然后调整您的 jQuery 以使用类 last_prop 而不是 ID,并从我们上面设置的 data 属性中获取值。

<script>
$(document).ready(function () {
    $('.last_prop').click(function () {
        var id = $(this).data('last-id');
        var user = $(this).data('last-user');

        $.ajax({
            url:"delete.php",
            method: "POST",
            data: {ilan_id: id, ilan_user: user},
            success:function(response){
                if (response == 1 ){
                    $('#last_prop').closest('tr').css('background','tomato');
                    $('#last_prop').closest('tr').fadeOut(800,function(){
                        $(this).remove();
                    });
                } else {
                    alert('Invalid id');
                }
            }
        });
    });
});
</script>

此外,您的查询可以减少到一个(您不需要SELECT),并且应该使用准备好的语句。

<?php
    require_once 'config.php';
    $id     =  $_POST['ilan_id'];
    $user   =  $_POST['ilan_user'];

    $sql = "DELETE FROM last_tbl WHERE id = ? AND user = ?;";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss", $id, $user);
    $stmt->execute();
    if ($stmt->affected_rows) {
        // rows were deleted
        echo 1;
    }
    $stmt->close();

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多