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

Link to comment
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.

 

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

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.