PHP AJAX删除记录-删除仅可使用1次

时间:2019-05-27 08:59:34

标签: javascript php jquery mysql ajax

我正在使用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;
    }

?>

1 个答案:

答案 0 :(得分:0)

您的问题是您要覆盖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();

相关问题