PHP MySQL。将 2 个表列链接在一​​起

发布于 2024-10-06 06:23:40 字数 705 浏览 0 评论 0原文

我有一个小问题,我正在尝试创建自己的论坛,但我陷入了困境。

我有 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 技术交流群。

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

发布评论

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

评论(2

我要还你自由 2024-10-13 06:23:40

您应该必须使用主表和事务表来管理数据库。

您的表格式应如下所示:

table name: user   

user_id user_name
   1       abc
   2       xyz  
   3       pqr
   4       new  


table2 name : forum_section

forum_id       user_id      forum_name
    1              4          abc
    2              4          jkl 
    3              2          cbd
    4              3          lmn

现在,您可以 JOIN 这两个表并根据需要进行查询,如下所示:

SELECT forum_id FROM forum_section as fs,user as u WHERE fs.user_id = u.user_id AND fs.user_id = 4

这可能对您有帮助。如果您需要更多帮助,请写信!

You should have to manage your database by using master and transaction tables.

Your table format should look like this:

table name: user   

user_id user_name
   1       abc
   2       xyz  
   3       pqr
   4       new  


table2 name : forum_section

forum_id       user_id      forum_name
    1              4          abc
    2              4          jkl 
    3              2          cbd
    4              3          lmn

Now, you can JOIN these two tables and make a query as you want, like this:

SELECT forum_id FROM forum_section as fs,user as u WHERE fs.user_id = u.user_id AND fs.user_id = 4

This may help you. Please write if you need more help!

九公里浅绿 2024-10-13 06:23:40

您需要使用 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.

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