Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何实现';喜欢';系统_Php_Mysql - Fatal编程技术网

Php 如何实现';喜欢';系统

Php 如何实现';喜欢';系统,php,mysql,Php,Mysql,我想为我的博客创建一个喜欢系统,让人们喜欢他们喜欢的帖子,所以我在blogs表中添加了一列,并将其命名为likes,但这样我就无法检测到哪个用户喜欢这篇帖子,我想为userID添加一列,但仅限于一列,有什么想法吗?您喜欢的人应该在他们自己的表中,以免违反数据库体系结构规则,特别是: 此时,由于唯一索引约束,您的表将自动拒绝任何插入。您喜欢的内容应该在自己的表中,以避免违反数据库体系结构规则,特别是: 此时,由于唯一索引约束,您的表将自动拒绝任何插入。您需要创建一个单独的表来保存postid和us

我想为我的博客创建一个喜欢系统,让人们喜欢他们喜欢的帖子,所以我在blogs表中添加了一列,并将其命名为likes,但这样我就无法检测到哪个用户喜欢这篇帖子,我想为userID添加一列,但仅限于一列,有什么想法吗?

您喜欢的人应该在他们自己的表中,以免违反数据库体系结构规则,特别是:


此时,由于唯一索引约束,您的表将自动拒绝任何插入。

您喜欢的内容应该在自己的表中,以避免违反数据库体系结构规则,特别是:


此时,由于唯一索引约束,您的表将自动拒绝任何插入。

您需要创建一个单独的表来保存postid和userid。这样,您可以有20个人喜欢该评论,但您可以查询以检查用户是否已经喜欢它。这是mysql表中的外观:

Table (comments)
-----------------
 PostID
 UserID
 Title 
 Content
然后是一张单独的表格,供您选择:

Table (likes)
-----------------
 LikeID (Just to make sure you have an index key)
 UserID (This would be the user id of user adding like)
 PostID  (This post id would be from the comments table)
 LikedDate (this is optional)
然后,在添加like之前,执行一个非常简单的sql查询,检查userid和postid是否已经在like表中。例如:

SELECT LikeID FROM likes_table WHERE UserID = ***** AND PostID = *****
然后,您只需在添加之前执行php查询:

if($rows_returned == 0){
 //add the like to likes table
}else{
 //throw an error that the like has already been added
}

希望这有帮助

您需要创建一个单独的表来保存postid&userid。这样,您可以有20个人喜欢该评论,但您可以查询以检查用户是否已经喜欢它。这是mysql表中的外观:

Table (comments)
-----------------
 PostID
 UserID
 Title 
 Content
然后是一张单独的表格,供您选择:

Table (likes)
-----------------
 LikeID (Just to make sure you have an index key)
 UserID (This would be the user id of user adding like)
 PostID  (This post id would be from the comments table)
 LikedDate (this is optional)
然后,在添加like之前,执行一个非常简单的sql查询,检查userid和postid是否已经在like表中。例如:

SELECT LikeID FROM likes_table WHERE UserID = ***** AND PostID = *****
然后,您只需在添加之前执行php查询:

if($rows_returned == 0){
 //add the like to likes table
}else{
 //throw an error that the like has already been added
}

希望这有帮助

这应该是一个单独的表,而不是posts表中的列。Likes表将包含
PostID
UserID
,它们分别是对posts表和user表的引用。研究数据库规范化这应该是一个单独的表,而不是posts表中的列。Likes表将包含
PostID
UserID
,它们分别是对posts表和user表的引用。学习数据库规范化完美,谢谢!太好了,谢谢!非常感谢。我不知道我怎么没想到这个谢谢你!我不知道我怎么没有想到这一点