PHP MySQL。将 2 个表列链接在一起
我有一个小问题,我正在尝试创建自己的论坛,但我陷入了困境。
我有 3 个表,1 个具有 user_level (身份验证级别)的用户。
forum_section 再次包含具有 user_level 的所有部分。
但是我如何将forum_section.user_level链接到forum_topics.user_level。
因此,如果我定义:forum_section.user_level = 4 for forum_section.section_id = 1。
例如:
有人想要访问forum_section id 1,那么他们的身份验证级别必须为4。 当他们转到主题时,它会再次检查用户级别是否为 4,但我不想手动设置主题级别,主题级别必须始终与部分级别相同。
我用谷歌搜索过这个,但我找不到一个好的手册。我想这与“外键”有关?
好吧,为了确定起见,让我再解释一下。 我有 3 个表
表 User
用户名=韦斯利
用户级别<例如 4(管理员)。
表格部分
部分 ID = 3
栏目名称 = 新闻
section_level = 3 <需要级别才能查看。
表格主题
主题 ID = 123
topic_name = 我喜欢糖果
topic_level = 需要与section_level相同,因此如果我更改section_level,它也会自动更改。
I've got an small issue, I'm trying to make my own forum, but I am stuck at something.
I have 3 tables, 1 users with a user_level (Authentication level).
forum_section which contains all the sections with a user_level again.
But how can I link forum_section.user_level to forum_topics.user_level.
So if I define: forum_section.user_level = 4 for forum_section.section_id = 1.
For example:
Someone wants to visit the forum_section id 1 then they must have a auth level of 4.
And when they go to the topic it then again checks if the user level is 4, but I do not want to manual set the topic level, topic level must always be the same as the section level.
I've googled for this, but I can't really find a good manual for it. I guess it has something to do with: "foreign keys"?
Okay, let me explain a bit more just to be sure.
I have 3 tables
Table User
username = Wesley
userlevel < for example 4(admin).
Table section
section_id = 3
section_name = News
section_level = 3 <require level to view.
table topics
topic_id = 123
topic_name = I like candy
topic_level = Needs to be the same as section_level so if I change section_level it automatically changes this too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该必须使用主表和事务表来管理数据库。
您的表格式应如下所示:
现在,您可以
JOIN
这两个表并根据需要进行查询,如下所示:这可能对您有帮助。如果您需要更多帮助,请写信!
You should have to manage your database by using master and transaction tables.
Your table format should look like this:
Now, you can
JOIN
these two tables and make a query as you want, like this:This may help you. Please write if you need more help!
您需要使用 SQL JOIN 或 嵌套子查询
最简单的实现是首先建立用户访问级别,然后过滤后续查询基于此 - 所以在你的 PHP 中,将访问级别传递给后续的内容请求 - 如果查询返回任何内容,用户可以查看内容,如果没有 - 他们可以被重定向。
You need to either use a SQL JOIN or a NESTED SUB QUERY
The simplest implementation would be to initially establish the users access level, then filter subsequent queries based on this- so in your PHP, pass the access level to subsequent requests for content- if the query returns anything, the user can view the content, if not- they can be redirected.