Jump to content

Adding user relations/friendship


mikhl

Recommended Posts

I am trying to add a friendship relation between users, but the friends ID always sets to 0 in the database.

 

Tables:

I have a members table that includes all my members information and a user_friendship table that relates each member.

 

user_friendship (friendship_id(PK), user_id(FK), friend_if(FK), status(default=0), date_created)

 

The users id is stored in a cookie called user_id and the friends id is stored in a cookie called v_user_id.

When I execute the following code, the 'friend_id' is set to 0, but everything else works:

 

$sql = "INSERT INTO user_friendship (user_id, friend_id, status) VALUES ('$_COOKIE[user_id]', '$_COOKIE[v_user_id]', '0')";
$result = mysql_query($sql);

 

Please help me, going nuts  :D

Link to comment
https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/
Share on other sites

your not accessing your $_COOKIE[] array correctly, you need to wrap the array field in quotes.

 

$sql = "INSERT INTO user_friendship (user_id, friend_id, status) VALUES ('$_COOKIE["user_id"]', '$_COOKIE["v_user_id"]"', '0')";
$result = mysql_query($sql);

 

Tried the above and got a syntax error on browser and on PHPEdit.

 

$user = $_COOKIE['user_id'];
$friend = $_COOKIE['v_user_id'];

$sql = "INSERT INTO user_friendship (user_id, friend_id, status) VALUES ('$user', '$friend', '0')";
$result = mysql_query($sql);

 

Also tried this way of doing it and the friend_id was still set as 0.

 

Sorry if I'm not understanding you.

your not accessing your $_COOKIE[] array correctly, you need to wrap the array field in quotes.

 

$sql = "INSERT INTO user_friendship (user_id, friend_id, status) VALUES ('$_COOKIE["user_id"]', '$_COOKIE["v_user_id"]"', '0')";
$result = mysql_query($sql);

 

Tried the above and got a syntax error on browser and on PHPEdit.

 

$user = $_COOKIE['user_id'];
$friend = $_COOKIE['v_user_id'];

$sql = "INSERT INTO user_friendship (user_id, friend_id, status) VALUES ('$user', '$friend', '0')";
$result = mysql_query($sql);

 

Also tried this way of doing it and the friend_id was still set as 0.

 

Sorry if I'm not understanding you.

 

Try:

 

$sql = "INSERT INTO user_friendship (user_id, friend_id, status) VALUES ($_COOKIE['user_id'], $_COOKIE['v_user_id'], '0')";
$result = mysql_query($sql)

 

I think that's what Muddy_Funster meant, if not, sorry for butting in :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.