twitter-follow

Creating a Follow User mechanism


I am trying to create a follow mechanism, where in a user can follow the other. What I am doing is if A tries to follow B, I insert A email id against B in follow table. Now when A again tries to follow B, A id is checked against B, if found true, it redirects and shows already followed but if A tries to follow someone whom A hasnt, it adds them.

Now the problem is, the first part is working fine, but if I follow the same person again, it gets entered again in the table.

$temp=$_GET['temp'];
echo $temp;

mysql_connect('localhost','root','');
mysql_select_db('reviewed');

$query="SELECT follow_user from follow where my_email= '".$_SESSION['email']."'";
$data=mysql_query($query);
if (!$data) { // add this check.
    die('Invalid query: ' . mysql_error()) ;
}
if(mysql_num_rows($data) > 0)
{
    while($row=mysql_fetch_array($data))
    {
        $user=$row['follow_user'];
        if($temp===$user)
        {
            header('Location:acq.php?success=1');
            $_SESSION['message'] = 'Already Following.';
        }
        else
        {
            mysql_connect('localhost','root','');
            mysql_select_db('reviewed');
            $query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
            $data=mysql_query($query);
            if (!$data) { // add this check.
                die('Invalid query: ' . mysql_error()) ;
            }           
            echo "Success";
        }       
    }
}
else
{
    mysql_connect('localhost','root','');
    mysql_select_db('reviewed');
    $query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
    $data=mysql_query($query);
    if (!$data) { // add this check.
        die('Invalid query: ' . mysql_error()) ;
    }           
    echo "Success"; 
}

Solution

  • It looks to me as though you are adding a follow record for every user other than B that A is following. I think you might be able to fix this by restricting to B as well as A in your initial query i.e.

    $query="SELECT follow_user from follow where my_email= '".$_SESSION['email']."' and follow_user = '".$temp"'";

    You can then remove the while loop:

    if(mysql_num_rows($data) > 0)
    {
        header('Location:acq.php?success=1');
        $_SESSION['message'] = 'Already Following.';
    
    }
    else
    {
        mysql_connect('localhost','root','');
        mysql_select_db('reviewed');
        $query="INSERT INTO follow (my_email, follow_user) VALUES('".$_SESSION['email']."','".$temp."')";
        $data=mysql_query($query);
        if (!$data) { // add this check.
            die('Invalid query: ' . mysql_error()) ;
        }           
        echo "Success"; 
    }