du512053619 2019-05-22 15:41
浏览 46

无法使用ajax php从数据库中删除数据

I've created a live table to show, edit and delete data from my database using Ajax, PHP, and MySQL. The problem is that my PHP code isn't executed and I have this error

 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!

The id_clasa is the name of one of the columns in my database.

My delete.php file

<?php

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

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

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

The script that displays the data from the database in a table

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

the ajax script(which is in 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>

If I use isset() or !empty() to check if they are declared before referencing them, the script seems to be executed but the data from the database is not deleted.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制