跟踪用户收藏夹的系统

编程入门 行业动态 更新时间:2024-10-08 22:47:22
本文介绍了跟踪用户收藏夹的系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的网站上,我有一个表movies和一个表users

On my website, I have a table movies and a table users

我正在尝试让用户单击添加到收藏夹"按钮,这会将电影添加到他的收藏夹中(目前不需要ajax/javascript,只需php).

I'm trying to have an "Add to favs" button that a user can click, which will add that movie to his favorites (ajax / javascript not necessary at the moment, just php).

那么我做这样的事情最简单的方法是什么?我已经考虑过这一点,但似乎找不到解决方案(我想到的只是太复杂了,我认为不可能).

So what's the simplest way I could do something like that? I've thought about this but I can't seem to find a solution (all I think of is way too complicated, and in my opinion not possible).

您有什么想法?

我不需要现成的脚本,只是一个可以使我工作的想法(尽管如果您有此类脚本的示例,我很乐意看一下).

I don't need a ready-made script, just an idea that could get me working (although if you have an example of such script, I'd be happy to look at it).

谢谢!

推荐答案

添加第三个表:

CREATE TABLE user_favorites ( user_id INT NOT NULL, movie_id INT NOT NULL, PRIMARY KEY (user_id, movie_id), FOREIGN KEY user_id REFERENCES users (user_id), FOREIGN KEY movie_id REFERENCES movies (movie_id) )

这称为交叉表或 join表,因为它将users表中的行连接到movies表中的行(如您所见,每列都是外键).它还定义了多对多关系,因为一个用户可以喜欢很多电影,而一部电影可以被许多用户喜欢.

This is called an intersection table or join table, as it joins rows in the users table to rows in the movies table (as you see, each column is a foreign key). It is also defines a many-to-many relationship, because one user can like many movies and one movie can be liked by many users.

当您要为用户添加喜爱的电影时,您要做的就是在此表中插入一行,其中包含用户ID和电影ID:

When you go to add a favorite movie for a user, all you have to do is insert a row in this table with the ID of the user and the ID of the movie:

INSERT INTO user_favorites(user_id, movie_id) VALUES([user ID], [movie ID])

要查看用户喜欢的电影:

To see what movies a user has favorited:

SELECT movie_id FROM user_favorites WHERE user_id = [user ID]

更多推荐

跟踪用户收藏夹的系统

本文发布于:2023-08-03 14:47:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收藏夹   用户   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!