Jump to content

[SOLVED] How add additional data to a cell?


sorenchr

Recommended Posts

Hi, i have a fairly simple table in my DB:

 

Username

Tags

testuser1

test, bla

testuser2

abc, bla2

 

Im using a php page, which allows the user to add additional tags to their username. So my question is, what mysql query do i use when i want to add additional data to their tags? I only know of UPDATE, but that one replaces all the existing data with the newly entered.

 

To illustrate my question, I, as "testuser1", add the tag "christmas" to my taglist, so that the new table looks like this:

 

Username

Tags

testuser1

test, bla, christmas

testuser2

abc, bla2

 

Best regards

Soren

Link to comment
Share on other sites

You shouldn't do it this way. The fact that you are having issues manipulating the data is a testimony to this. What you're looking at is a many-to-many relationship which is made by having a third linking table giving you these tables:

 

users

- user_id

- username

 

tags

- tag_id

- name

 

tags_users

- tag_id

- user_id

 

To get user #568's tags you can do this:

SELECT t.name
  FROM tags_users link
  INNER JOIN users u
    ON u.user_id = link.user_id
  INNER JOIN tags t
    ON t.tag_id = link.tag_id
  WHERE u.user_id = 568;

 

And as for your problem, adding a tag to a user is a simple INSERT operation (you'd have to insert a new tag into the tags table as well if it's not a new tag).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.