doushang8846 2017-07-20 17:47
浏览 48
已采纳

Sweet Alert显示成功,但记录未删除

PHP code

$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get members");

$data = array();

while($row = mysqli_fetch_array($query)) { // preparing an array

    $schoolID   = $row["schoolID"];
    $schoolname = $row["schoolname"];
    $program    = $row["program"];

    $nestedData = array();
    $nestedData[] = $row["schoolname"];
    $nestedData[] = $row["level"];
    $nestedData[] = $row["program"];
    $nestedData[] = $row["startdate"];
    $nestedData[] = $row["duration"];
    $nestedData[] = "   <a href='upDateSchool.php?schoolID=$schoolID' class='btn btn-warning fa fa-pencil'>
                        </a>
                            |           
                        <a class='btn  btn-danger' id='delete_school' data-id='<?php echo schoolID=$schoolID$schoolID; ?>',  href='javascript:void(0)'><i class='glyphicon glyphicon-trash'></i></a>
                    ";  

JavaScript

 <script>
  $(document).ready(function(){


    $(document).on('click', '#delete_school', function(e){

      var schoolID = $(this).data('id');
      SwalDelete(schoolID);
      e.preventDefault();
    });

  });

  function SwalDelete(schoolID){

    swal({
      title: 'Are you sure?',
      text: "Record Deleted is Irreversible!",
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#d33',
      cancelButtonColor: '#3085d6',
      cancelButtonText: "No!",
      confirmButtonText: 'Yes, delete it!',
      showLoaderOnConfirm: true,

      preConfirm: function() {
        return new Promise(function(resolve) {

           $.ajax({
            url: 'deleteSchool.php',
            type: 'post',
              data: 'delete='+schoolID,
              dataType: 'json'
           })
           .done(function(response){
            swal('Deleted!', response.message, response.status);
          readProducts();
           })
           .fail(function(){
            swal('Oops...', 'Something went wrong with ajax !', 'error');
           });
        });
        },
      allowOutsideClick: false        
    }); 

  }



</script>

PHP code for deleting

<?php
header('Content-type: application/json; charset=UTF-8');

$response = array();    

$sid = $_POST['delete'];
$query = "DELETE FROM school where schoolID=  :schoolID";
$stmt = $db->prepare( $query );
$stmt->execute(array(':schoolID'=>$sid));

if ($stmt) {
    $response['status']  = 'success';
    $response['message'] = 'School Deleted Successfully ...';
} else {
    $response['status']  = 'error';
    $response['message'] = 'Unable to delete School ...';
}
echo json_encode($response);    

When I click the delete button, it shows response success but the record does is not deleted.

  • 写回答

1条回答 默认 最新

  • dongqing4070 2017-07-20 18:00
    关注

    Your code contains many logical and syntax problems, but still.
    The problem is, that you exactly don't print out ID in data-id attribute.
    Try to change this:

    $nestedData[] = "   <a href='upDateSchool.php?schoolID=$schoolID' class='btn btn-warning fa fa-pencil'>
                        </a>
                            |           
                        <a class='btn  btn-danger' id='delete_school' data-id='<?php echo schoolID=$schoolID$schoolID; ?>',  href='javascript:void(0)'><i class='glyphicon glyphicon-trash'></i></a>
                    ";
    

    To this:

    $nestedData[] = "   <a href='upDateSchool.php?schoolID=".$schoolID."' class='btn btn-warning fa fa-pencil'>
                        </a>
                            |           
                        <a class='btn  btn-danger' id='delete_school' data-id='".$schoolID."',  href='javascript:void(0)'><i class='glyphicon glyphicon-trash'></i></a>
                    ";
    

    It may help, but I recommend you to perform refactoring of your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码