-1

PHP 代码

$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 代码

<?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);    

当我单击删除按钮时,它显示响应成功,但记录没有被删除。

4

1 回答 1

0

您的代码包含许多逻辑和语法问题,但仍然存在。
问题是,您完全没有在data-id属性中打印出 ID。
尝试改变这一点:

$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>
                ";

对此:

$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>
                ";

它可能会有所帮助,但我建议您对代码进行重构。

于 2017-07-20T18:00:20.430 回答