Changing follow to unfollow on click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harman30
    New Member
    • Sep 2015
    • 19

    Changing follow to unfollow on click

    I am trying a create a follow system, with this code but the follow button doesn't change to unfollow on click. I manually tried inserting values in database through phpmyadmin (so that one user now follows another). I should now see Unfollow but still shows as Follow. My db connection is in header file so I am using include header file.

    Code on profile.php
    Code:
       <?php include ( "./inc/header.inc.php" ); ?>
    
         <?php
           function is_loggedin()
         {
         return (!empty($_SESSION['user_login']));
         }
        function is_profile($user = false)
         {
         if(!$user)
            return false;
    
          return (is_loggedin() && ($_SESSION['user_login'] == $user));
          } 
       // If no user requested just assign false
         $user   =   (!empty($_GET['u']) && ctype_alnum($_GET['u']))? $_GET['u'] : false;
        // If the user is valid (not empty)
    
        if($user != false) {
    
        $username   =   mysql_real_escape_string($user);
        //check user exists
        $check      =   mysql_query("SELECT username, first_name FROM users WHERE username='$username'");
    
        if (mysql_num_rows($check) === 1) {
                $get        =   mysql_fetch_assoc($check);
                $username   =   $get['username'];
                $firstname  =   $get['first_name'];  
            }
        else {
                echo "<meta http-equiv=\"refresh\" content=\"0; url=http://localhost/tutorials/index.php\">"; 
                exit;
            }
            }
    
          $my_id = $_SESSION['user_login'];
          $u_id = $user;
          ?>
    
         <?php
         if($u_id !=$_SESSION['user_login']) {
         $check = mysql_query("SELECT id FROM follow WHERE user_one='$my_id' AND user_two='$u_id'");
         if(mysql_num_rows($check) == 1){
          echo "<a href='actions.php?action=Unfollow&u_id=$u_id'>Unfollow</a>";
           }
          else{
           echo "<a href='actions.php?action=Follow&u_id=$u_id'>Follow</a>";
           }
          }
         ?>
    Code for actions.php
    Code:
      
       <?php include ( "./inc/header.inc.php" ); 
    
       $my_id = $_SESSION['user_login'];
         $u_id = $user;
        ?>
    
       <?php
       $action = $_Get['action'];
       $u_id = $_GET['u_id'];
    
        if($action == 'Follow'){
       mysql_query("INSERT INTO follow VALUES ('','$my_id','$u_id')");
        }
          if($action == 'Unfollow'){
             mysql_query("DELETE FROM follow WHERE user_one='$my_id' AND  user_two='$u_id'");
        }
       header('Location: profile.php?u='.$u_id);
       ?>
Working...