doushang8846 2017-07-20 09: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 10: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部