Home >Backend Development >PHP Tutorial >PHP development: How to implement the article like function
PHP development: How to implement the article like function, specific code examples are required
Introduction:
In modern social networks and web applications, the like function has already Become a common social interaction feature. Whether it is news, Weibo, blogs or community forums, users can express their love and support for the content by liking it. This article will introduce how to use PHP to develop and implement the article like function, and give specific code examples.
1. Design the database structure
Before starting to implement the article like function, you first need to design the database structure. We can create two tables, one to store article information and the other to store like information.
2. Back-end logic to implement the like function
The back-end logic to implement the like function mainly includes two parts: adding likes and canceling like.
PHP code example:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); // 获取文章ID和用户ID $articleId = $_POST['articleId']; $userId = $_POST['userId']; // 查询点赞表 $query = "SELECT * FROM like WHERE article_id = $articleId AND user_id = $userId"; $result = mysqli_query($conn, $query); // 判断是否已经点赞过该文章 if (mysqli_num_rows($result) > 0) { // 用户已经点赞过该文章,不执行任何操作 } else { // 用户没有点赞过该文章,插入一条新记录 $insertQuery = "INSERT INTO like (article_id, user_id) VALUES ($articleId, $userId)"; mysqli_query($conn, $insertQuery); } // 关闭数据库连接 mysqli_close($conn); ?>
PHP code example:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); // 获取文章ID和用户ID $articleId = $_POST['articleId']; $userId = $_POST['userId']; // 查询点赞表 $query = "SELECT * FROM like WHERE article_id = $articleId AND user_id = $userId"; $result = mysqli_query($conn, $query); // 判断是否已经点赞过该文章 if (mysqli_num_rows($result) > 0) { // 用户已经点赞过该文章,删除相关记录 $deleteQuery = "DELETE FROM like WHERE article_id = $articleId AND user_id = $userId"; mysqli_query($conn, $deleteQuery); } else { // 用户没有点赞过该文章,不执行任何操作 } // 关闭数据库连接 mysqli_close($conn); ?>
3. Front-end implementation of the like function
Add a like button to the front-end page and interact with the back-end. You can use Ajax to Send an asynchronous request.
HTML code example:
<button class="like-button" data-article-id="1">点赞</button>
JavaScript code example:
// 点赞按钮点击事件 $(".like-button").on("click", function() { var articleId = $(this).data("article-id"); var userId = 1; // 假设用户ID为1 // 发送异步请求到后端 $.ajax({ type: "POST", url: "like.php", data: { articleId: articleId, userId: userId }, success: function(response) { // 更新点赞按钮状态 $(this).addClass("active"); alert("点赞成功!"); } }); });
IV. Summary
Through the above implementation steps, we can achieve article likes in PHP development Function. From the writing of back-end logic to the implementation of front-end interaction, we can achieve the effect of users liking articles and getting feedback on the interface. I hope this article can be helpful to the implementation of the like function in PHP development.
Special Note:
The code examples in this article are for reference only. Please make appropriate modifications and optimizations according to specific conditions during the actual development process to ensure the security and performance of the code.
The above is the detailed content of PHP development: How to implement the article like function. For more information, please follow other related articles on the PHP Chinese website!