mikhl Posted January 19, 2011 Share Posted January 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/ Share on other sites More sharing options...
Muddy_Funster Posted January 19, 2011 Share Posted January 19, 2011 your not accessing your $_COOKIE[] array correctly, you need to wrap the array field in quotes. Quote Link to comment https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/#findComment-1162161 Share on other sites More sharing options...
Maq Posted January 19, 2011 Share Posted January 19, 2011 mikhl, do NOT double post please. Moving this to PHP Coding b/c it seems more appropriate, if not I will move it back. Quote Link to comment https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/#findComment-1162171 Share on other sites More sharing options...
mikhl Posted January 19, 2011 Author Share Posted January 19, 2011 I'm sorry. Made a mistake in where I thought it should go. Wont happen again. Quote Link to comment https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/#findComment-1162175 Share on other sites More sharing options...
mikhl Posted January 19, 2011 Author Share Posted January 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/#findComment-1162176 Share on other sites More sharing options...
TOA Posted January 19, 2011 Share Posted January 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/#findComment-1162178 Share on other sites More sharing options...
mikhl Posted January 19, 2011 Author Share Posted January 19, 2011 Sorry tool a while to reply. I have figured out the problem. My code was ok, I think. But the path on my cookie restricted the use of it, so changing it allowed me to use it. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/225006-adding-user-relationsfriendship/#findComment-1162228 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.