-1

I am working on a personal project and I'm trying to create a php script to display a follow button if the logged in session user doesn't already follow the user displayed..

Heres what I've got:

<?php
      $query = query("SELECT*FROM following, users WHERE followed_id=" .escape_string($_GET['id'])." 
      AND user_id=".escape_string($_GET['id'])." AND follower_id =".escape_string($_SESSION['user_id'])."");
      confirm($query);
      while($row = fetch_array($query)):

       if(mysqli_num_rows($query)==0){

         $followbutton=<<<DELIMITER

       <form method="post" action="">
         <input type="hidden" name="followed" value="{$row['user_id']}">
         <input type="hidden" name="follower" value="{$_SESSION['user_id']}">
         <input class="btn btn-info" type="submit" name="submit" value="follow">
       </form>

DELIMITER;
       echo $followbutton;

    }elseif($row['follower_id'] == $_SESSION['user_id']){
      $unfollowbutton=<<<DELIMITER

    <form method="post" action="unfollow.php?id={$row['user_id']}">
      <input class="btn btn-info" type="submit"  value="Unfollow">
    </form>

DELIMITER;
    echo $unfollowbutton;

  }
endwhile;    ?>

Here's what query, escape_string, fetch_array and confirm do:

function redirect($location){
header("Location: $location");
}

function query ($sql){
global $connection;
return mysqli_query($connection,$sql);
}

function confirm($result) {
global $connection;
if(!$result){
    die("QUERY FAILED ".mysqli_error($connection));
}
}

function escape_string($string){
global $connection;
return mysqli_real_escape_string($connection,$string);
 }

 function fetch_array($result) {
return mysqli_fetch_array($result);
}

The "following" table only has two columns; followed_id and follower_id. When the user clicks the follow button it passes the logged in user_id as the follower_id and the user_id for the person being followed as the followed_id. Since there wont be any entries if user A hasn't already followed user B I need the button to display only if no results are returned from the query.

9
  • Welcome to SO! Please edit your question to specify exactly what's not working with a minimal example (stackoverflow.com/help/minimal-reproducible-example for reference). Is there a bug in what you currently have, or are you wondering how to achieve some specific functionality? Is there actually a conditional not working as expected, or does this issue have something to do with outputting/submitting a form, connecting to the database, etc. Oct 17, 2019 at 18:06
  • Error reporting/logs show what, in regards to possible errors? What isn't working the way you want it to? Oct 17, 2019 at 18:08
  • Remove the double quotes in AND follower_id =".escape_string($_SESSION['user_id'])."") at the end
    – nbk
    Oct 17, 2019 at 18:09
  • also, try to put space after SELECT in SELECT*FROM following
    – Mubin
    Oct 17, 2019 at 18:10
  • There aren't any errors I just can't get it to function the way I want it to. If user A is following user B the 'unfollow' button displays properly on user B's profile. However I also want the 'follow' button to display instead if there aren't any results returned from the sql query.
    – tokenthr
    Oct 17, 2019 at 18:11

1 Answer 1

2

If I understand your question correctly, you either want to display a follow button or an unfollow button. In that case, you can use a simple if/else statement. Currently, your elseif statement will only be true when a row is returned because you're checking for its existence: if ($row['follower_id'] == $_SESSION['user_id']). I now see that your elseif is checking if the user exists. I had it backwards. Everything else in my answer still stands.

For what's it is worth, you really should be using prepared statements to prevent SQL injection attacks. Also, I think your code could be simplified a bit to make it clearer.

  • Not sure why you're selecting from both the following and users ables. You only need to check if the following table has you (the logged in user) and the person you're checking against, no?
  • No need for that while loop. You can simply count the rows. If there are > 0 rows, you know that you're following the person already. In theory I guess, there should only be 1 row.

Here's an example of how I'd write this. I'm using PDO here simply because that's my go to API, but you can continue to use mysqli.

<?php
$stmt = $pdo->prepare("SELECT count(*) FROM following WHERE follower_id = ? AND followed_id = ?");
$stmt->execute([
    $_SESSION['user_id'], // logged-in user
    $_GET['id']           // who we are following
]);

// How to count when using PDO - https://phpdelusions.net/pdo_examples/count
$count = $stmt->fetchColumn();

if ($count > 0) {
    // show `unfollow` form
} else {
    // show `follow` form
}
1
  • Wooooooow my dude. This worked a treat and I'm going to switch to using prepared statements. A+ explanation of where I was going wrong too. What a guy!
    – tokenthr
    Oct 17, 2019 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.