dongyuan8469 2012-10-12 12:30
浏览 25
已采纳

为用户设置权限的最佳方式

(Using PHP + mySQL)

I'm hoping someone can give me an idea as to the best way to handle this situation.

I have two tables, users and forms. The users table holds username, password, name, etc. and then the forms table holds various different html forms.

I want to specify which forms a user has access to, on a per user basis. I was thinking about creating a new field in the users table which would be 'forms_allowed' and then storing the form_id of each form the user is allowed to access, separated by commas. Something like 3,6,8,15

Then when I want to check what forms the user is allowed to access and whether they can access the form requested (etc.) i'd grab that field, explode it and check for the form_id.

This seems like a hassle and ineffective way of doing it. Does anyone have a better idea?

Thanks!

  • 写回答

1条回答 默认 最新

  • dsf6565 2012-10-12 12:33
    关注

    Create another table that will hold user_id and form_id pair. This is many-many relationship.

    tblUsers:

    user_id | name |...
    

    tblForms

    form_id | content | ...
    

    tblRelation:

    user_id | form_id
    

    So you can simply get info with single query:

    SELECT * FROM `tblUsers` AS `t`
    JOIN `tblRelation` as `r` ON (t.user_id = r.user_id)
    JOIN `tblForms` as `f` ON (f.form_id = r.form_id)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用