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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀