PHP/MySQL Array Problem?

Michael Smith

New member
I'm trying to program a simple social network and right now I am coding the part to store your friend list in a MySQL database. Here is my code thus far:
//Various DB connect info that I have left out
$friendid = $_POST['frienduser'];
$myusername = $_COOKIE['login'];
$sql = "SELECT FROM login (friends) WHERE username='$myusername'";
$friendlist = mysql_query($sql);
if($friendlist == "" or $friendlist == NULL) {
$friendlist = array($friendid);
}
else {
$friendlist = unserialize($friendlist);
$friendlist[] = $friendid;
}
$friendlist = serialize($friendlist);
$newsql = "UPDATE login SET friends='$friendlist' WHERE username='$myusername'";
mysql_query($newsql);
header("location:login_success.php");

It will write the serialized array to the Database but the problem is that it doesn't append the new friend to the array. It overwrites the currently stored array so that I am left with only one friend, the friend I got from the form post. If anyone could give me a solution to this problem it would be greatly appreciated. Thanks.
The username was gotten from a cookie that I accessed in the code I cut off.
 
Back
Top