justlukeyou Posted December 23, 2012 Share Posted December 23, 2012 (edited) Hi, A member of this forum kindly provided code which I was doing wrong. The purpose of the code is to create a 'social network' which enables people to follow other members by inserting their ID number into a database. I was inserting the ID number of the user into the same table as the main user table. However someone pointed out that I suggest use a seperate table and insert the ID number of both users into the table. The problem is I cant get the code to work. I can echo the id but this only comes from the first line of code. Nothing else seems to work. I have tried manually inserting one of the id's to see if they would help. I am trying to insert id 354 as the follower of 350. Any suggestions what I can try please? <?php $id = $_SESSION['userID']; if (($_GET['do'] == 'follow') && !empty($_GET['id'])) { // check if user is logged in if (($_SESSION['auth']) && !empty($_SESSION['current_user_id'])) { // whatever your $_SESSION variable is for logged in users if ($_SESSION['current_user_id'] == $_GET['current_user_id']) { // other checks here to determine various ID's are numeric, etc. $sql = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES (". (int)$_SESSION['current_user_id'] .", ". (int)$_GET['id'] .")"; if (!mysql_query($sql)) { if (mysql_errno($link_identifier) == 1062) { //$link_identifier is necessary to avoid conflicting error notices due to multiple openning/closing SQL connections // duplicate attempt to follow // handle accordingly } } } } } ?> </div> <div class="forminputcell"> <div class="datainput"> <div class="forminputleft"> Follow: </div> <div class="followbutton"> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?do=follow&id=354"><img src="/images/follow.png" class="submit-button"/></a> </div> </div> </div> </div> <?php echo $id; ?> <br> <?php echo $current_user_id; ?> <br> </div> Edited December 23, 2012 by justlukeyou Quote Link to comment https://forums.phpfreaks.com/topic/272320-struggling-with-provided-follow-code/ Share on other sites More sharing options...
twistedvengeance Posted December 24, 2012 Share Posted December 24, 2012 <?php $id = $_SESSION['userID']; if (($_GET['do'] == 'follow') && !empty($_GET['id'])){ if(($_SESSION['auth']) && !empty($_SESSION['current_user_id'])){ if ($_SESSION['current_user_id'] == $_GET['current_user_id']){ $you = intval($_SESSION['current_user_id']); $them = intval($_GET['id']); $sql = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('$you', '$them')"; if (!mysql_query($sql)){ if (mysql_errno($link_identifier) == 1062) { /* Why is this even here? */ } } } } } ?> Your problem was you didn't have your variables escaped with ' '. so you were saying VALUES(code,code) not VALUES('code','code'); Quote Link to comment https://forums.phpfreaks.com/topic/272320-struggling-with-provided-follow-code/#findComment-1401140 Share on other sites More sharing options...
Pikachu2000 Posted December 24, 2012 Share Posted December 24, 2012 If the values are numeric, which they presumably should be as indicated by the comments in the code, then they should not be "escaped" with quotes in the query. They should be validated and left unquoted. Quote Link to comment https://forums.phpfreaks.com/topic/272320-struggling-with-provided-follow-code/#findComment-1401142 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2012 Share Posted December 24, 2012 (edited) @twistedvengeance, The two values are integers, not strings, and they don't need to be enclosed by single-quotes in the query. That actually just makes mysql work harder because numbers enclosed by single-quotes are first converted to floating point numbers. Edited December 24, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/272320-struggling-with-provided-follow-code/#findComment-1401143 Share on other sites More sharing options...
justlukeyou Posted December 24, 2012 Author Share Posted December 24, 2012 Hi, I changed it to this and it now inserts the current_user_id into the database. However Im struggling to understand how I can seperate out the two different id numbers. If I click on the profile of 350 I have the ID 350. But how do I seperate that from ID 355? Whatever I do have the ID showing as 355 if I am logged is as 355. <?php $id = $_SESSION['userID']; $user_id = $_SESSION['userID']; $current_user_id = $_SESSION['userID']; if (($_GET['do'] !== 'follow') || (int)$_GET['id']) { // just send away if ($_SESSION['current_user_id'] == $_GET['current_user_id']) { // other checks here to determine various ID's are numeric, etc. $sql = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES (". (int)$_SESSION['current_user_id'] .", ". (int)$_GET['id'] .")"; if (!mysql_query($sql)) { if (mysql_errno($link_identifier) == 1062) { //$link_identifier is necessary to avoid conflicting error notices due to multiple openning/closing SQL connections // duplicate attempt to follow // handle accordingly } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272320-struggling-with-provided-follow-code/#findComment-1401161 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.