Reputation: 566
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.
Upvotes: 0
Views: 89
Reputation: 6112
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'
Upvotes: 4