如何从使用 jQuery 选择的数据库行中删除


how to delete from database row selected with jquery

我有以下代码:

文件讲师.php:

$(document).ready(function(){
             $(".use-address").click(function() {
             var $row = $(this).closest("tr");    // Find the row
             var names = $row.find(".name").text(); // Find the name
             var surname = $row.find(".surname").text(); // Find the surname                
                $.ajax({
                    type: "POST",
                    url: "delete_lecturer.php",
                    data:{ x: names, y: surname}
                });
             });
        });

文件delete_lecturer.php:

<?php
ob_start(); //eliminates buffer collisions
    require_once('connect_db.php'); 
    if (!empty($_POST['x'])){   $name = $_POST['x']; }
    if (!empty($_POST['y'])){   $surname = $_POST['y']; }
    $result = pg_query(connect(), "delete from lecturer where name='$name' and surname='$surname'");    
    echo "did it";
    //dump the result object
    var_dump($result);
    // Closing connection
    //reloading the page
    header("location: lecturer.php?fail=2");
?>

文件connect_db.php工作(我已经用其他需要它的文件测试了它)。我正在尝试从表中获取名称和姓氏,将其传递给delete_lecturer.php文件,然后从数据库中删除与我的选择匹配的行。jquery 代码中的名字和姓氏被正确存储(我也测试过)。

但是代码仍然不起作用。任何想法出了什么问题或如何解决它? 顺便说一下,我使用postgresql作为数据库。

你可以执行以下代码:

           $.ajax({  type: "POST",  
                url: "delete_lecturer.php",
                data: { x: names, y: surname}
            })
            .done(function( msg ) {
                msg = $.trim(msg);
                // Use alert or console.log()
                alert(msg);
                console.log(msg);
            })
            .fail(function( msg ) {
                // Use alert or console.log()
                alert(msg);
                console.log(msg);
            });
使用该代码,

您可以检查您的代码在哪里出错,因为它似乎没有任何问题