close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Many to many relationship

Thread began 12/17/2011 4:49 am by steelace396915 | Last modified 12/20/2011 10:22 pm by steelace396915 | 1960 views | 4 replies |

steelace396915

Many to many relationship

I wonder how to accomplish the following with DataAssist:

1. I have an eductaion site where users/students should be able to access different media (pdf and mp3 files etc). Some files should be accessible to all users, some to just a few and so on. I have set up one MySQL table named "users" for all the userdata and one named "media" for the media files. I have also created a inbetween table consisting just of 2 fields, UserID reflecting the user table primary key field UserID and MediaID reflecting the media table primary key field MediaID.

2. I have built all the login, logout, registration functionallity for users with SecurityAssist and it works great now. I have also built an administration section with users update and file uploading to the media tabel in database.

3. A user who logs in successfully is transfered to the students page where he/she should be able to access his/her registration information and mediafiles which he/she has access to. I want to be able to show/list only certain files for different users but don't know how to accomplish this?

For example: Several users should maybe have access to the same mediafile and sometimes just one user should be able to access a certain mediafile. For what I have learned I need to have the Many-to-many relational table I described above to achieve this? I just don't understand how to set this binding up on the page? Someone please give me some input in how to achieve this!?

/Martin Johannesson

Sign in to reply to this post

Jason ByrnesWebAssist

you can use a join query to bind the usermedia and media tables together

SELCT media.*
FROM media
INNER JOIN usermedia.MediaID = media.Media.ID



when a user logs in, the users ID is stored in a session variable.

edit your join query to add a where clause that will return the media only for the logged in user. Lets say for example that the name of the User session is "UserID":

WHERE usermedia.UserID = paramUID

then in the variables section, create a new variable:

Name: paramUID
Type: Number
Default value: -1
Runtime value: $_SESSION['UserID']


for more details on joined queries, see the following:
sql_join.asp

Sign in to reply to this post

steelace396915

Thanks Jason! I thought I needed a linking table like I described in post? This seems easier really but I don't know exaclty why! ;=) I'm a real newbie to PHP and MySQL yet but I'm learning new stuff every day!

I tried the code you gave me but I get error messege when testing the page:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.Media.ID WHERE users.UserID = -1' at line 1


My 2 tables are named: users and media
Table media has an ID field named: MediaID
In the users table I created a field named: UserMediaID

Come to think about it, I do have a separate table named accesslevels where I store the different levels (Admin, student, visitor). It has 2 fields, 1 ID field named AccessLevelsID and 1 named AccessLevelsName. In the users table I also have a field named UserAccessLevelsID then.
This is the problem right?


Regards,
Martin J.

Sign in to reply to this post

Jason ByrnesWebAssist

well, the example i gave you _is_ using the linking table, but you dont need the data from all three tables to return the files that are associated to the users. you only need to use the linking table and the media table.


also bear in mind, that the recordset code i gave is not exact, you need to modify it based on your table and column names (This is info I dont have so cant give you exact SQL syntax).



assuming you have a users table, media table and usersmedia (the linking table) with the following basic structure:

users:
userID - primaryKey

media:
mediaID - primary KEY

usermedia:
usermediaID - primary key
usermediaUserID - relates to users.userID
usermediaMediaID - relates to media.mediaID

you can use the following SQL:
SELECT media.*
FROM media
INNER JOIN usermedia.usermediaMediaID = media.media.ID
WHERE usermedia.usermediaUserID = paramUID


I would recommend getting some books from the local bookstore on the MySQL Subject to become a little more familiar with this subject matter, there is only so much the internet can teach you.

Sign in to reply to this post

steelace396915

Thanks a lot Jason! I think I understand this now! Will definitely pick up some MySQL book and learn this from scratch!

I wish you and the others at Webassist a merry Christmas and a happy new 2012.

Regards,
Martin Johannesson

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...