Jump to content

Recommended Posts

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 by justlukeyou
Link to comment
https://forums.phpfreaks.com/topic/272320-struggling-with-provided-follow-code/
Share on other sites

<?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');

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.

@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 by PFMaBiSmAd

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
						    }
				    }
		    }
}
?>

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.