无法使用Ajax PHP从数据库中删除数据

时间:2019-05-22 15:41:52

标签: javascript php jquery

我创建了一个活动表,以使用Ajax,PHP和MySQL从数据库中显示,编辑和删除数据。问题是我的PHP代码未执行,并且出现此错误

 Undefined index: id_clasa in <b>E:\xampp2\htdocs\Licenta (v.final)\admin\clase_zbor\delete.php</b> on line <b>5</b><br />
        Date sterse!

id_clasa是数据库中列之一的名称。

我的delete.php文件

<?php

    require('../../connect.php');

    $sql="DELETE FROM clasa WHERE id_clasa='".$_POST['id_clasa']."'";

    if(mysqli_query($conn,$sql)){
        echo 'Date sterse!';
    }
?>

该脚本在表中显示数据库中的数据

<?php

   require('../../connect.php');

    $output='';
    $sql="select * from clasa order by id_clasa";
    $result=mysqli_query($conn,$sql);
    $result_check=mysqli_num_rows($result);

    $output.='
        <div class="table-responsive">
            <table class="table table-striped text-center table-bordered">
                 <thead class="thead-dark">
                   <tr>
                      <th>ID</th>
                       <th>Nume_clasa</th>
                       <th>Action</th>
                      </tr>
                 </thead>';

if($result_check > 0){
    while($row=mysqli_fetch_array($result)){
        $output.='<tr>
                <td>'.$row["id_clasa"].'</td>
                <td class="nume_clasa" data-id1="'.$row["id_clasa"].'" contenteditable>'.$row["nume_clasa"].'</td>
                <td><button type="button" name="delete_btn" data_id2="'.$row["id_clasa"].'" class="btn btn-danger btn_delete" >Delete</button></td>
                </tr>
            ';
    }

    $output.='<tr> 
                <td></td>
                <td id="nume_clasa" contenteditable></td>
                <td><button class="btn btn-success" name="btn_add" id="btn_add" class="btn btn-xs btn-succes">Add</button></td>
            </tr>';

}
else{
    $output.='<tr>
                <td colspan="3">Nu exista date</td>
                </tr>';
}

$output.='</table>
            </div>';

echo $output;
?>

ajax脚本(位于flight_class.php中)

<script>

$(document).ready(function(){
   function fetch_data()
      {
       $.ajax({
           url:"select.php",
           method:"POST",
           success:function(data){
               $('#live_data').html(data);
           }
       });
   }

   fetch_data();

    $(document).on('click','#btn_add', function(){
       var nume_clasa= $('#nume_clasa').text();
       if(nume_clasa == ''){
           alert("Introdu numele clasei");
           return false;
           }
       $.ajax({
           url:"insert.php",
           method:"POST",
           data:{nume_clasa:nume_clasa},
           dataType:"text",
           success:function(data){
               alert(data);
               fetch_data();
           }
       });
   });

    function edit_data(id_clasa, text, nume_clasa){
        $.ajax({
            url:"edit.php",
            method:"POST",
            data:{id_clasa:id_clasa, text:text,nume_clasa:nume_clasa},
            dataType:"text",
            success:function(data){
                alert(data);
            }
        });
    }

    $(document).on('blur','.nume_clasa', function(){
        var id=$(this).data("id1");
        var nume_clasa=$(this).text();
        edit_data(id, nume_clasa, "nume_clasa");
    });


    $(document).on('click','.btn_delete', function(){
        var id_clasa=$(this).data("id2");
        if(confirm("Are you sure?")){
            $.ajax({
                url:"delete.php",
                method:"POST",
                data:{id_clasa:id_clasa},
                dataType:"text",
                success:function(data){
                    alert(data);
                    fetch_data();
                }
            });
        }
    }); 
});

    </script>

如果我在引用它们之前使用isset()!empty()来检查它们是否已声明,则该脚本似乎已执行,但不会删除数据库中的数据。

0 个答案:

没有答案