1

(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!

0

1 Answer 1

4

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)
2
  • this will also enable you to add and remove rights to a single form for an user. It's the most common way of doing it. Oct 12, 2012 at 12:35
  • Excellent, thank you so much. Back to work today and this is the route i'll take. Cheers!
    – Stevie
    Oct 15, 2012 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.