Question

I'm fairly new to MySQL and PHP, but I'm getting the hang of it slowly.

I'm working on a site with profile pages. I have a users table in my database and I intend to create a profile table too.

Before I explain my 'problem' I'd like to say I haven't actually began coding an attempted solution. I like to plan my work before I start it, especially when it is going to be complex. Therefore, I can't really display any snippets of code.

What I would like to create is a tagging system for the user profiles. ie. let the profile owner select keywords from an existing list, and associate them with his profile page.

After pondering plenty and reading up on relational databases I gathered I should start by creating two additional tables:

1. tags (tag_id, tag_name)
2. tag_rel (tag_id, user_id)

and create a relationship between each profile to the different tags a user assigned to it, on seperate rows:

(Tag1, UserA)
(Tag2, UserA)
(Tag1, UserB)

etc.

Then when I search for profiles I simply have to select all user_ids with a specific tag_id. Easy enough.

My issue is the PHP and MYSQL Query code when inserting or changing the list.

I would essentially want the user to be able to either choose from a check-list or from a drop-down select list (or anything equivalent) of existing Tags, check or uncheck his selection, and send to enter it to the database.

I can imagine the initial entry of the selected tags is easy enough to achieve with a foreach() function and a simple INSERT query, and enter the array from the form one value at time.

But when the user goes back to change the tags, how would I instruct a query to keep some tags, delete others, and insert new ones in? What is the best method to do something like this?

An Idea: Should I delete all the existing rows the user has in tag_rel, and insert the new array?

Was it helpful?

Solution

I would use a procedure like this: at first you fetch the tags a user selected out of the db and pass them to an array (SELECT from tag_rel where user_id = $userId)

then you would go over to let the user operate on the array (adding new tags, deleting old one)

at last you would delete the old entries and insert the completely new ones from the array provided

OTHER TIPS

the simplest method is to delete tag_rel data for that particular user_id then re-insert the new ones

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top