跟踪用户收藏夹的系统 [英] System for keeping track of user favorites

查看:93
本文介绍了跟踪用户收藏夹的系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我有一个表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]

这篇关于跟踪用户收藏夹的系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆