用于跟踪用户收藏夹的系统

发布于 2024-09-19 11:45:04 字数 299 浏览 3 评论 0原文

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

我试图有一个用户可以单击的“添加到收藏夹”按钮,这将添加该按钮电影到他的最爱(目前不需要ajax/javascript,只需php)。

那么我可以做这样的事情的最简单的方法是什么?我已经考虑过这个问题,但我似乎找不到解决方案(我想到的都太复杂了,而且在我看来不可能)。

你有什么想法?

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

谢谢!

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

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).

What's your thoughts?

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).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮生未歇 2024-09-26 11:45:05

这是一个多对多的关系。一个用户可以收藏很多部电影,一部电影也可以被很多用户收藏。在 RDBMS 中,您表示与第三个表的多对多关系。我将其称为交集表,但它也有其他名称。

创建一个包含两列的表。这些列都是外键,分别引用电影和用户。

CREATE TABLE 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)
); 

当用户选择喜欢某部电影时:

INSERT INTO Favorites (user_id, movie_id) VALUES (?, ?)

当用户决定不再喜欢某部电影时,删除相应的行:

DELETE FROM Favorites WHERE (user_id, movie_id) = (?, ?)

获取给定用户喜欢的电影集:

SELECT movie_id FROM Favorites WHERE user_id = ?

获取喜欢给定电影的用户集电影:

SELECT user_id FROM Favorites WHERE movie_id = ?

关于您的评论之一:

您不应该将“添加到收藏夹”作为链接。像谷歌这样的索引器会跟踪链接,然后在你意识到之前,每个用户都喜欢了每部电影。

一般的最佳实践是只读操作可以是 GET 请求,而写入数据库的操作可以是 POST 请求。这意味着您需要使用

元素来提交 POST 请求,而不是 元素。

This is a many-to-many relationship. A user can favorite many movies, and a movie can be favored by many users. In an RDBMS, you represent a many-to-many relationship with a third table. I call this an intersection table but it goes by other names too.

Create a table with two columns. The columns are both foreign keys, referencing movies and users, respectively.

CREATE TABLE 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)
); 

When a user chooses to favorite a movie:

INSERT INTO Favorites (user_id, movie_id) VALUES (?, ?)

When a user decides they don't like a movie any longer, delete the corresponding row:

DELETE FROM Favorites WHERE (user_id, movie_id) = (?, ?)

To get the set of movies favored by a given user:

SELECT movie_id FROM Favorites WHERE user_id = ?

To get the set of users who favor a given movie:

SELECT user_id FROM Favorites WHERE movie_id = ?

Regarding one of your comments:

You shouldn't make the "Add to favorite" a link. Indexers like Google will follow links, and then before you know it, every user has favorited every movie.

The general best practice is that read-only operations can be GET requests, while operations that write to the database can be POST requests. This means that you need to use a <form> element to submit POST requests, not an <a href="..."> element.

唠甜嗑 2024-09-26 11:45:05

添加第三个表:

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)
)

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

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

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

要查看用户喜欢的电影:

SELECT movie_id FROM user_favorites WHERE user_id = [user ID]

Add a third table:

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)
)

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.

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]
烟─花易冷 2024-09-26 11:45:05

您需要创建一个新表:

user_favorite_movies
--------------------
ID (primary key)
userID (foreign key)
movieID (foreign key)
date

然后,当用户单击“添加收藏夹”按钮时,您只需在 user_favorite_movies 中插入一个新行,其中包含用户表中的用户 ID、电影表中的电影 ID 以及日期已添加(有利于稍后排序)。

希望这有帮助!

最好的,

-埃里克

You will need to create a new table:

user_favorite_movies
--------------------
ID (primary key)
userID (foreign key)
movieID (foreign key)
date

Then when the user clicks the 'Add Favorite' button, you just insert a new row into user_favorite_movies with the users ID from the user table, the movie id from the movie table, and the date it was added (good for sorting later).

Hope this helps!

Best,

-Eric

尸血腥色 2024-09-26 11:45:05

您可以创建一个包含三列的表 favouritesidmiduid。添加收藏:

INSERT INTO favourites (mid, uid) VALUES (3, 5)

搜索一位用户的收藏:

SELECT * FROM favourites WHERE uid = 7

搜索收藏一部电影的人:

SELECT * FROM favourites WHERE mid = 9

You could create a table favourites with three columns, id, mid and uid. To add a favourite:

INSERT INTO favourites (mid, uid) VALUES (3, 5)

To search for favourites of one user:

SELECT * FROM favourites WHERE uid = 7

To search for people who favourited one movie:

SELECT * FROM favourites WHERE mid = 9
年少掌心 2024-09-26 11:45:05

据我所知,您仍然需要使用 JavaScript 或 Ajax 来发布帖子,除非您想在每次标记/取消标记收藏夹时刷新页面,并添加/删除新的收藏夹指示器同时。

或者我错过了什么?

So far as I can see, you'll still need to use JavaScript or Ajax to do the post, unless you want to refresh the page every time thet mark/unmark a favorite, and also to add/remove the new favorite indicator in place at the same time.

Or am I missing something?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文