为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用ajax和php制作删除功能

如何使用ajax和php制作删除功能

PHP
心有法竹 2021-11-05 14:53:18
我不知道我的代码有什么问题,但我有这个错误注意:未定义索引:删除我想用 CRUD 做删除功能这是我的 php 代码:    if($_POST["delete"]){    $query = '    DELETE FROM `users` WHERE id = :id    ';    $statement = $connect->prepare($query);    $statement->execute(        array(            ':id'              => $_POST['id']        )    );    $result = $statement->fetchAll();    if(isset($result))    {        echo '<div class="alert alert-fill-danger" role="alert">User Deleted!<div>';    }}这是 AJAX 函数$(document).on('click', '#delete', function(){       var id = $(this).data('id');       $('#message').html('');       Swal.fire({  title: 'Are you sure?',  text: "You want to delete this user?",  type: 'warning',  showCancelButton: true,  confirmButtonColor: '#3085d6',  cancelButtonColor: '#d33',  confirmButtonText: 'Yes'  }).then((result) => {       if (result.value){        $.ajax({         url:'/auth/action',         method:'POST',         data:{id:id},         success:function(data)         {          if(data != '')          {           load_user_data();           $('#message').html(data);          }         }        });                      Swal.fire(      'User Deleted!',      '',      'success'    )       }       })      });我不知道我在这里错过了什么,但是 php 中的功能不起作用??
查看完整描述

2 回答

?
Helenr

TA贡献1780条经验 获得超4个赞

这是我的问题的答案:D


var action = 'delete';

我只是在 ajax 中的 id 下添加此代码


if($_POST["action"] == 'delete')

    {

        $query = '

        DELETE FROM `users` WHERE id = :id

        ';

        $statement = $connect->prepare($query);

        $statement->execute(

            array(

                ':id'              => $_POST['id']

            )

        );

        $result = $statement->fetchAll();

        if(isset($result))

        {

            echo '<div class="alert alert-fill-danger" role="alert">User Deleted!<div>';

        }

    }


查看完整回答
反对 回复 2021-11-05
?
holdtom

TA贡献1805条经验 获得超10个赞

也许尝试以下操作: 在尝试在 PHP 中使用变量之前测试它们的可用性。此外 - 将没有记录集,因此fetchAll将无法工作。


if( isset( $_POST['delete'], $_POST['id'] ) {

    ob_clean();

    $id=$_POST['id'];


    $query = 'DELETE FROM `users` WHERE id = :id';

    $statement = $connect->prepare( $query );

    $result = $statement->execute( array( ':id' => $id ) );


    if( $result && $statement->rowCount() > 0 ){

        echo '<div class="alert alert-fill-danger" role="alert">User Deleted!<div>';

    }

    exit();

}


查看完整回答
反对 回复 2021-11-05
  • 2 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信