用于跟踪用户收藏夹的系统
在我的网站上,我有一个表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个多对多的关系。一个用户可以收藏很多部电影,一部电影也可以被很多用户收藏。在 RDBMS 中,您表示与第三个表的多对多关系。我将其称为交集表,但它也有其他名称。
创建一个包含两列的表。这些列都是外键,分别引用电影和用户。
当用户选择喜欢某部电影时:
当用户决定不再喜欢某部电影时,删除相应的行:
获取给定用户喜欢的电影集:
获取喜欢给定电影的用户集电影:
关于您的评论之一:
您不应该将“添加到收藏夹”作为链接。像谷歌这样的索引器会跟踪链接,然后在你意识到之前,每个用户都喜欢了每部电影。
一般的最佳实践是只读操作可以是 GET 请求,而写入数据库的操作可以是 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.
When a user chooses to favorite a movie:
When a user decides they don't like a movie any longer, delete the corresponding row:
To get the set of movies favored by a given user:
To get the set of users who favor a given movie:
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.添加第三个表:
这称为交集表或连接表,因为它将
users
表中的行连接到>movies
表(如您所见,每一列都是一个外键)。它还定义了一种多对多关系,因为一个用户可以喜欢多部电影,而一部电影也可以被许多用户喜欢。当您要为用户添加最喜欢的电影时,您所要做的就是在此表中插入一行,其中包含用户的 ID 和电影的 ID:
要查看用户喜欢的电影:
Add a third table:
This is called an intersection table or join table, as it joins rows in the
users
table to rows in themovies
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:
To see what movies a user has favorited:
您需要创建一个新表:
然后,当用户单击“添加收藏夹”按钮时,您只需在 user_favorite_movies 中插入一个新行,其中包含用户表中的用户 ID、电影表中的电影 ID 以及日期已添加(有利于稍后排序)。
希望这有帮助!
最好的,
-埃里克
You will need to create a new table:
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
您可以创建一个包含三列的表
favourites
:id
、mid
和uid
。添加收藏:搜索一位用户的收藏:
搜索收藏一部电影的人:
You could create a table
favourites
with three columns,id
,mid
anduid
. To add a favourite:To search for favourites of one user:
To search for people who favourited one movie:
据我所知,您仍然需要使用 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?