0

I'm trying to do a MySQL Query that adds information to a database in a format that doesn't overwrite it's current value, but instead appends it as in the friends column would have friend1, friend 2, friend 3 ... etc.

First I am unsure if INSERT INTO is right:

$username = $_SESSION['username'];

mysql_query("INSERT INTO members (friends) WHERE username = $username
    VALUES ('$friendtoadd')");

What I want to do is have this do two things

1) Add the friend in a format mentioned so that it can later be called out that if the $username set in session is active then the results posted on the page are only from those other users contained in their friends column.

2) If the $friendtoadd already exists in their column for friends then it does nothing.

Dadsquatch
  • 566
  • 5
  • 16
  • 1
    Can you give the table schema that you expecting to insert ? – Ruwantha Jun 29 '12 at 04:20
  • look at this.It should be of use to you.http://stackoverflow.com/questions/2761583/appending-data-to-a-mysql-database-field-that-already-has-data-in-it – vijay Jun 29 '12 at 04:21

1 Answers1

4
UPDATE members SET friends = CONCAT_WS(', ', friends, '$friendtoadd') where username = '$username'

I hope $friendtoadd as well as $username are sanitized through mysql_real_escape_string

Also, this design isn't very good. You should have a seperate friends table so

table friend
id (auto increment), userID, name)

then do 
Insert into friend SET userID = $userID, name = '$friendName'
Kris
  • 6,094
  • 2
  • 31
  • 46