$ .get没有错误但不起作用

时间:2017-09-19 09:17:38

标签: php jquery mysql ajax

我正在尝试创建一个员工管理系统,现在我设法创建了一个填充了一些数据库内容的表。

现在我想要的是在点击位于每行末尾的按钮时删除所选行。 要做到这一点,我正在尝试使用Jquery $ .get方法,它调用一个名为delete.php的文件来完成这项工作,然后设置动画并从html中删除该行。

问题是:动画启动,但php文件不会从数据库中删除记录!我甚至尝试使用console.log来查找错误,但正如您在下面的屏幕截图中看到的那样,它只是给了我页面的html代码而不是错误代码......

screenshot

这是我用来生成表格的代码:

<?php 

            $sql = "SELECT * FROM dipendente";

            $result = $conn->query($sql);

            if ($result->num_rows > 0) {


            echo "<table class='table table-striped table-hover'>".
                        "<thead>".
                            "<tr>".
                                "<td>id</td>".
                                "<td>nome</td>".
                                "<td>cognome</td>".
                                "<td>impiego</td>".
                                "<td>permessi</td>".
                                "<td>contratto</td>".
                                "<td>inizio</td>".
                                "<td>fine</td>".
                                "<td>edit</td>".
                            "</tr>".
                        "</thead>";

                echo "<tbody>";

                // output data of each row
                while($row = $result->fetch_assoc()) {

                    $id = $row["id"];

                    echo "<tr id=".$row["id"].">".
                            "<td>" . "#". $row["id"]. "</td>" . 
                            "<td contenteditable='false'>" . $row["nome"]. "</td>".
                            "<td contenteditable='false'>" . $row["cognome"] . "</td>".
                            "<td contenteditable='false'>" . $row["impiego"] . "</td>".
                            "<td contenteditable='false'>" . $row["permessi"] . "</td>".
                            "<td contenteditable='false'>" . $row["contratto"] . "</td>".
                            "<td contenteditable='false'>" . $row["inizio"] . "</td>".
                            "<td contenteditable='false'>" . $row["fine"] . "</td>".
                            "<td>" . 
                                "<span class='glyphicon glyphicon-pencil' aria-hidden='true'></span>" . 
                                "&nbsp;" . 
                                "<span class='glyphicon glyphicon-remove-sign' aria-hidden='true'></span>" .
                                "<a href='#' id='$id' class='delete'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></a>".
                                "&nbsp;".
                                "<span class='glyphicon glyphicon-ok-sign' aria-hidden='true'></span>".
                            "</td>".
                        "</tr>";
                }

                echo "</tbody>";

                echo "</table>";


            } else {
                echo "0 results";
            }

这是我的jquery代码:

  $(".delete").click(function(){

    var del_id = $(this).attr("id");
    var tr = $(this).parent().parent();

    console.log(del_id);
    console.log(tr);


    $.get("php/delete.php", { id: del_id } , function(error){
        //console.log(error);
        tr.fadeOut("1s",function(){
            $(this).remove();
        });

    });

});

并且有我的delete.php代码:

<?php

$id = $_GET['del_id'];

$dbc = mysqli_connect('localhost', 'root', 'root', 'gestione_personale') or die('Connection error!');

$query = "DELETE FROM dipendente WHERE id = '$id'";

mysqli_query($dbc, $query) or die('Database error!');

header('location:../index.php');

我做错了吗?

2 个答案:

答案 0 :(得分:0)

根据我所看到的,我将给出这个答案。但是你真的需要使用mysqli_*和你使用它的方式一起下车。我给你的答案是准备声明,它比你使用它的方式更安全。

$dbc = mysqli_connect('localhost', 'root', 'root', 'gestione_personale') or die('Connection error!');

$query = $dbc->prepare("DELETE FROM dipendente WHERE id = ?");
$query->bind_param("i", $_GET['id']);

if($query->execute()){
    header('location:../index.php');
}else{
    die('Database error!')
}

希望这适合你。

答案 1 :(得分:0)

尝试在网址delete.php中访问,将del_id作为arg传递,如下所示:delete.php?del_id=1

然后你会看到记录是否被删除或是否有错误,还要检查是否启用了php display_errors

相关问题