使用 PDO 和 SQL 将 post 添加到收藏夹

Adding post to favorite with PDO and SQL

我已经使用 PHPSQL 实现了 添加到收藏夹,这样用户就可以在数据库中保存收藏夹 post 和 删除最喜欢的表格

我使用了下面的代码:

    function checkFavorite($mbid, $pid, $conn) {
        $stmt = $conn->prepare("SELECT * FROM favorite WHERE memberID=:mid AND id=:id");
        $stmt->execute(array(':mid' => $mbid, ':id' => $pid));
        $count = $stmt->rowCount();
        if ($count == 0) {
            echo "<div class='button' data-method='Like' data-user-id=" . $mbid . " data-director-id=" . $pid . "><i class='mi' id=" . $pid . ">favorite_border</i>Add Favorite</div>";
        } else {
            echo "<div class='button' data-method='Unlike' data-user-id=" . $mbid . " data-director-id=" . $pid . "><i class='mi mi_sml text-danger' id=" . $pid . ">favorite</i>Remove Favorite</div>";
        }
    }

    $email = 'user@user.com';
    // Query to get the user_id
    $stmt = $conn->prepare('SELECT memberID FROM members WHERE email = :email AND active="Yes" ');
    $stmt->execute(array(':email' => $email));
    $row = $stmt->fetch();
    $mbid = $row['memberID'];

    $pid = '4';
    // Query to Get the Director ID
    $stmt = $conn->prepare('SELECT * FROM allpostdata WHERE id =:id');
    $stmt->execute(array(':id' => $pid));
    $result = $stmt->fetchAll();
    foreach ($result as $row) {

        echo "<p>Director: " . $row['tit'] . "</p> ";
        $fav_image = checkFavorite($mbid, $pid, $conn);
        echo "Favorite? : " . $fav_image . "";
    }

脚本

        jQuery(document).ready(function ($) {
            $('.button').on('click', function (e) {
                e.preventDefault();
                const user_id = $(this).attr('data-user-id');
                const director_id = $(this).attr('data-director-id');
                const method = $(this).attr('data-method');
                if (method === "Like") {
                    $(this).attr('method', 'Unlike'); // Change the div method attribute to Unlike
                    $('#' + director_id).replaceWith('<i class="mi mi_sml text-danger" id="' + director_id + '">favorite</i>Remove Favorite'); // Replace the image with the liked button
                } else {
                    $(this).attr('method', 'Like');
                    $('#' + director_id).replaceWith('<i class="mi mi_sml" id="' + director_id + '">favorite_border</i>Add Favorite');
                }
                $.ajax({
                    url: 'favs.php', // Call favs.php to update the database
                    type: 'GET',
                    data: {user_id: user_id, director_id: director_id, method: method},
                    cache: false,
                    success: function (data) {
                    }
                });
            });
        });

当你看图片时你可以看到

  1. replaceWith 不更改按钮和文本
  2. 并且在 XHR 请求中所有请求方法都发送为 method="like"

但是当发送请求时,数据被保存并且控制台没有错误。

那么如何在单击按钮时将 Add to favorite 的文本更改为 Remove from favorite

我想这就是您所需要的!这是我几年前在演示中使用的,您可以根据您的要求进行编辑。

将您的更新 url 添加到像我这样的短信中并删除。

$(document).ready(function () {

    $(".like button").click(function () {
        if ($(this).hasClass('btn')) {
            $(this).html('remove').toggleClass('btn mybtn');
        } else {
            $(this).html('Like me').toggleClass('mybtn btn');
        }
    });
});
.mybtn {
    color: red;
}
.btn {
    color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="like">
    <p>Like</p>
    <button class="btn">Like me</button>
</div>

Demo

更新: 抱歉我离开了电脑!注入您的代码应该非常简单:

我猜你的 php 代码在你的 favs.php 我确实尝试过这样的响应 favs.php echo 'response';

Html 按钮!您可以更改为 class :

$(document).ready(function () {
 $(".button").click(function (e) {
 e.preventDefault();
 const user_id = $(this).attr('data-user-id');
 const director_id = $(this).attr('data-director-id');
 const method = $(this).attr('data-method'); 
        if (method === "Like") {
            $(this).html('<i class="mi mi_sml text-danger" id="' + director_id + '">favorite</i>Remove Favorite').toggleClass('button mybtn');
            //Change remove text to your i class
        } else {
            $(this).html('<i class="mi mi_sml" id="' + director_id + '">favorite_border</i>Add Favorite').toggleClass('mybtn button');
            //Change Like me text to your i class 
            //you can remove or edit class mybtn in both remove and like
        }
 $.ajax({
  url: 'favs.php',
  type: 'GET', // type not method
  data: {user_id: user_id, director_id: director_id, method: method},
  cache: false,
  success: function(data) {
   alert(data);
  }
 });
});
});
.mybtn {
    color: red;
}
.btn {
    color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="like">
    <p>Like</p>
    <button type="submit" class="btn">Like me</button>
</div>

我可能有一些错误,例如 class 名称等...您可以编辑并使其适合您的要求。

我测试了它的工作正常。 SOLVED