结果与我期望的结果不一样

时间:2017-07-09 09:28:56

标签: javascript php jquery ajax

现在我想要取消关注按钮,如果有人已经跟随,那么不显示关注按钮,浏览器应该显示取消关注按钮而不是像twitter这样的按钮。我刚刚测试了(取消关注按钮出现),但没有完美运行。这是因为当我从(user1)在线并且按照某个工作并按下按钮更改为取消关注但是当我从(user2)在线时,想要跟随其后的同一个人(user1)..来自(user2)时间轴它显示已经跟随(按钮)请检查代码

if(isset($_POST['follow']))
        {
            $checkFollow=mysqli_query($con,"SELECT follower_id FROM follower WHERE user_id='$userid'");

            if(mysqli_num_rows($checkFollow)==0)
            {
                $follow=mysqli_query($con,"INSERT INTO follower(user_id,follower_id) VALUES ('$userid','$followerid')");
                echo "You Followed";
            }
            else
            {
                echo "Sorry! You already followed this profile";
            }
            $isFollowing=True;
        }

        $checkFollow=mysqli_query($con,"SELECT follower_id FROM follower WHERE user_id='$userid'");
        if(mysqli_num_rows($checkFollow))
        {
            $isFollowing=True;
        }

    }

HTML代码

  <form action="<?php echo $username;?>" method="post">                              
                       <?php
                             if($isFollowing)
                             {
                             echo "<input type='submit' value='Unfollow' name='unfollow' class='unfollow_button'>";
                             }
                             else
                             {
                                echo "<input type='submit' value='Follow' name='follow' class='unfollow_button'>"; 
                             }

                             ?>
                         </form>

1 个答案:

答案 0 :(得分:0)

我尝试根据您的初始代码猜测您的数据库结构并提出这个答案:

if(isset($_POST['follow']))
{
    $id = $_GET['id'];
    $user_query = mysqli_query($con,"SELECT id FROM user_reg WHERE user_name='$id'");  // ($id) is actually $_GET variable

    if(mysqli_num_rows($user_query) > 0) { //make sure you have at least one result for that $id
        $row = mysqli_fetch_assoc($user_query); //fetch the database result
        $userid = $row['id']; //assign the value to the $userId variable
    } else { // die if no user found with that $id
        die("User id not found");
    }

    $followerid=$idd;  // ($idd) is current session (User Logged in)
    $checkFollow=mysqli_query($con,"SELECT follower_id FROM follower WHERE user_id='$userid'");

    if(mysqli_num_rows($checkFollow) == 0) //check if no rows resulted in the $checkFollow query
    {
        //VERY IMPORTANT!!! make sure the column names are correct. You were using follower_id and user_id in the $checkfollow query and the followerid and userid in the INSERT query
        $follow=mysqli_query($con,"INSERT INTO follower (user_id,follower_id) VALUES ('$userid','$followerid')"); 
        echo "Followed";
    }
    else
    {
        die("User already followed");
    }
}


?>

<form action="test.php?id=<?php echo $username;?>" method="post">
    <input type="submit" value="Follow" name="follow" class="follow_button">
</form>

我希望这会有所帮助