使用Ajax从MySQL删除记录

时间:2018-07-08 05:59:31

标签: php jquery mysql ajax crud

我的代码有问题,我无法成功运行它。当我使用 Google Developer工具调试Ajax代码时,Ajax 数据参数具有主键( uid )的值,但似乎没有不会向 delete.php 发送 POST 请求。我不知道问题是什么。 谢谢您的帮助和建议!

index.php:按Delete按钮触发Ajax代码

<tbody>
<!--Populate HTML Table--> 
<?php if(!empty($records)) { 
foreach ($records as $record) {
?>
<tr>
    <td data-target="rowNum"></td>
    <td data-target="userId" style="display: none;">
        <?php echo $record['uid']; ?> 
    </td>
    <td data-target="firstname"><?php echo $record['first_name']; ?></td>   
    <td data-target="lastname"><?php echo $record['last_name']; ?></td>
    <td data-target="emailaddress"><?php echo $record['email_address']; ?></td>
    <td>
        <button class="btnEdit">Edit</button>
        <!--Press Delete Button to Fire Ajax Code-->
        <button class="btnDelete">Delete</button>
    </td>
</tr>
<?php } ?>
<?php } ?>
</tbody>

Ajax代码:将userId(表主键)作为数据发送到Delete.php

$(document).ready(function(){
    $(".btnDelete").click(function(){

        var userId = $(this).closest("tr").children("td[data-target=userId]").text();
        $.ajax({
            url: "delete.php",
            type: "POST",
            data: userId
        });

    });
});

delete.php:

<?php 

include 'database.php';

$conn->query("DELETE FROM Users WHERE uid = '$_POST['userId']'");

?>

3 个答案:

答案 0 :(得分:0)

您像这样传递数据:

$.ajax({
    url: "delete.php",
    type: "POST",
    data: userId
});

userId值可能只是您要在delete.php中使用的没有密钥的标量。因此,将此代码更改为:

$.ajax({
    url: "delete.php",
    type: "POST",
    data: { userId: userId }
});

它应该可以工作。

答案 1 :(得分:0)

这可能会有所帮助,您正在以文本形式发送数据。

    $(document).ready(function(){
    $(".btnDelete").click(function(){

        var userId = $(this).closest("tr").children("td[data-target=userId]").text();
        $.ajax({
            url: "delete.php",
            type: "POST",
            contentType: 'text/plain'
            data: {userId:userId}
        });

    });
  });

答案 2 :(得分:0)

您可能可以按照您尝试的方式运行它,但这对我来说更有意义:

<tbody>
<!--Populate HTML Table--> 
<?php if(!empty($records)) { 
foreach ($records as $record) {
?>
<tr>
    <td data-target="rowNum"></td>
    <td data-target="firstname"><?php echo $record['first_name']; ?></td>   
    <td data-target="lastname"><?php echo $record['last_name']; ?></td>
    <td data-target="emailaddress"><?php echo $record['email_address']; ?></td>
    <td>
        <button class="btnEdit">Edit</button>
        <!--Press Delete Button to Fire Ajax Code-->
        <form class="deleteForm" method="POST">   
            <input type="hidden" name="userId" value="<?= $record['uid'] ?>" />
            <button type="submit" class="btnDelete">Delete</button>
        </form>
    </td>
</tr>
<?php } ?>
<?php } ?>
</tbody>

然后用JavaScript

$(document).ready(function(){
    $(".deleteForm").on('submit', function(){
        var data = $(this).serialize();  // Let jQuery prepare the data
        $.ajax({
            url: "delete.php",
            type: "POST",
            data: data
        });
        return false; // Cancel default

    });
});

最后:

<?php 

include 'database.php';
$userId = filter_input(INPUT_POST, 'userId', FILTER_VALIDATE_INT); //Assuming an ID is an int, in general be cautions of SQL injection.

if ($userId) {
    $conn->query("DELETE FROM Users WHERE uid = '$_POST['userId']'");
} else {
   http_response_code(400); // Missing the input
   die();
}


?>