Reputation: 123
I've been racking my brain over this for a few days now, and being fairly new to 'back end' stuff, I haven't managed to come up with a table structure that works. What I'm trying to do is create a wish list feature which allows users to save a course to their profile for later. Originally, I created three fields in my 'users' table, named: 'saved_course1', 'saved_course2' and 'saved_course3'. I then tried to come up with a php/sql code that when a user clicks the wish list button on a courses page, the id of the course is saved to the one of the saved courses fields (providing that it isn't already filled), however someone pointed out to me that this would be an terrible way of doing this and it would mean my db wouldn't be normalised. So to sum my problem up, I need help with creating a table structure that would allow users to save multiple course id's to their account, then allow me to get the id's of the courses and display them in a table on their profile page. Sorry if this makes no sense, as I'm finding it difficult to put it into words.
Upvotes: 0
Views: 1047
Reputation: 293
That's not so difficult just make this table structure below. Let me know if you don't understand it
Example
User(userid,name,etc)
Courses(coursesid,name,fee, etc)
WhishList(userid,coursesid)
userid is foreign key to User, null not allowed
coursesid is foreign key to Courses, null not allowed
Extract courses for particular user
select * from WhishList join courses on Whishlist.coursesid = Courses.coursesid where WhishList.userid = "USER_ID HERE";
Upvotes: 2