gpt4 book ai didi

php - 使用户能够收藏帖子

转载 作者:行者123 更新时间:2023-11-29 13:06:51 25 4
gpt4 key购买 nike

在我的网站上,我希望允许用户收藏帖子。登录的用户被定向到一个显示所有帖子的页面,在每个帖子下我都放置了一个指向收藏夹的超链接。我希望文本从最喜欢变为最喜欢,反之亦然。我该怎么做?

HTML 和 PHP

<?php
session_start();
require_once('connection.php');

mysql_select_db($database_connection, $connection);
$query_favorite = "SELECT username, post_id FROM favorite";
$favorite = mysql_query($query_favorite, $connection) or die(mysql_error());
$row_favorite = mysql_fetch_assoc($favorite);
$totalRows_favorite = mysql_num_rows($favorite);
?>

<a href="#" class="favourite">Favourite</a>

我的数据库中的表

CREATE TABLE `user` (
`username` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`profilepic` varchar(50) DEFAULT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `post` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`dato` date NOT NULL,
`category` varchar(100) NOT NULL,
`description` varchar(500) NOT NULL,
`text` longtext NOT NULL,
PRIMARY KEY (`post_id`),
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

CREATE TABLE `favorite` (
`username` varchar(45) NOT NULL,
`post_id` int(11) NOT NULL,
PRIMARY KEY (`username`,`post_id`),
KEY `fk_favorite_post1_idx` (`post_id`),
CONSTRAINT `fk_favorite_user` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_favorite_post1` FOREIGN KEY (`post_id`) REFERENCES `post` (`post_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8

最佳答案

PHP

<?php
session_start();
require_once('connection.php');

mysql_select_db($database_connection, $connection);
$query_favorite = "SELECT username, post_id FROM favorite";
$favorite = mysql_query($query_favorite, $connection) or die(mysql_error());
$row_favorite = mysql_fetch_assoc($favorite);
$totalRows_favorite = mysql_num_rows($favorite);

if(in_array($_POST['id'], $row_favorite))
{
//is already favourited, run a query to remove that row from the db, so it won't be favorited anymore

}
else
{
//post is not favourited already, run a query to add a new favourite to the db.
}

?>

HTML

<a href="#" class="favourite" data-id="<?php echo $post_id; ?>">Favourite</a>

jQuery

$(document).ready(function() {
$('.favourite').on('click', null, function() {
var _this = $(this);
var post_id = _this.data('id');
$.ajax({
type : 'POST',
url : '/file.php',
dataType : 'json',
data : 'id='+ post_id,
complete : function(data) {
if(_this.siblings('.typcn-star-outline'))
{
_this.html('<span class="typcn typcn-star-full-outline"></span>Favourite');
}
else
{
_this.html('<span class="typcn typcn-star-outline"></span>Favourited');
}
}
});
});
});

另外,请注意,在 PHP 中,mysql_* 函数已被弃用并且使用不安全(它们允许 SQL 注入(inject)攻击)。在此处了解有关 PDO 的更多信息:http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

关于php - 使用户能够收藏帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22583300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com