AJAX调用PHP文件,从数据库中删除一行

AJAX to call PHP file which removes a row from database?

本文关键字:删除 一行 数据库 PHP 调用 文件 AJAX      更新时间:2023-09-26

好的,昨天我问了一个关于如何保存用户的博客文章的问题。我解决了数据库方面的问题,这很好。现在,我想删除一个博客文章后,点击一个onclick按钮。通过对web的几个小时的研究,我发现调用jQuery AJAX函数是最好的方法。我一直在摆弄它,但是我不能使它工作。

Blog .php中从数据库中检索的Blog代码:

$connection = mysql_connect("...", "...", "...") or die(mysql_error());
$database = mysql_select_db("...") or die(mysql_error());
$query = mysql_query("SELECT * FROM template") or die(mysql_error());
$template = mysql_fetch_array($query);
$loop = mysql_query("SELECT * FROM content ORDER BY content_id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($loop))
{
        print $template['Title_Open'];
        print $row['title'];
        print '<button class="deletePost" onClick="deleteRow(' . $row['content_id'] . ')">Remove Post</button>';
        print $template['Title_Close'];
        print $template['Body_Open'];
        print $row['body'];
        print $template['Body_Close'];
}
mysqli_close($connection);

这会在home.php上创建以下HTML:

<div class="blogtitle" class="post3">Title
<button class="deletePost" onClick="deleteRow(3)">Remove Post</button></div>
<div class="blogbody" class="post3">Content</div>

当按钮被点击时应该调用我的remove.js(这是我开始失去我正在做的事情):

$function deleteRow(id){
    $.ajax({
        url: "remove.php",
            type: "POST",
            data: {action: id}
    });
    return false;         
};

调用remove.php(不知道我在干什么):

$con=mysqli_connect("...","...","...","...");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_POST['action'];
$query = mysql_query("DELETE FROM content WHERE content_id=$id") or die(mysql_error());

我这里的目标是从表中删除具有ID的行,这将反过来完全删除博客文章,因为它在循环遍历数据库表时不会看到该行。

任何想法?

谢谢你的帮助,凯尔

原始代码中的几个问题:Jquery中的函数不应该在开始时使用$符号,因为您需要传递单个值,所以我会使用查询字符串而不是POst,而不是在php中调用"die",我会使用受影响的行来返回是否删除值的回调。但这只是我的方法,我相信还有其他方法。

下面是你代码中的一些小改进:

    //HTML
    <div class="blogtitle" class="post3">Title
    <button class="deletePost" data-item="3" >Remove Post</button></div>
    <div class="blogbody" class="post3">Content</div>
   //JQUERY
    jQuery(document).ready(function($) {
        $('button.deletePost').each(function(){
            var $this = $(this);
            $this.click(function(){
                var deleteItem  = $this.attr('data-item');
                $.ajax({url:'remove.php?action='+deleteItem}).done(function(data){
                    //colect data from response or custom code when success
                });
             return false;  
            });
        });

    });

   //PHP
    <?php 
    $id = $_REQUEST['action'];
    $query = mysql_query('DELETE FROM content WHERE content_id="'.$id.'"');
    $confirm = mysql_affected_rows() > 0 ? echo 'deleted' : echo 'not found or error';
     ?>

希望这个示例对您有所帮助:)快乐编码!

我希望这应该帮助你我用这个从我的购物车项目中删除项目。

$(".deleteitem").each(function(e) {
            $(this).click(function(e) {
                $.post("library/deletefromcart.php",{pid:$(this).attr('prodid'), ajax:true},function(){
                    window.location.reload()
                })
                return false;
             });
        });