doubu4826 2015-02-24 15:20
浏览 14
已采纳

PHP帮助:在微博中关注系统

I'm making a site similar to Instagram. I am very new to php. I created a follow button in the user's profile.

How do you make the follow button disappear when you already followed the user? How do you replace it with unfollow button?

// my php code for following

if (isset($_POST['addfriend'])){


    $fromuser = $user;
    $touser = $username;

    if($fromuser == $username){
        $Msg = "You cannot follow yourself<br/>";
    }
    else
    {
        $getID= mysql_query("SELECT userID FROM user WHERE username='$user'");
        $get_ID_row = mysql_fetch_assoc($getID);
        $ID_db = $get_ID_row['userID'];

        $sql = "insert into following (userID, fromUser, toUser) 
        values ('$ID_db','$fromuser', '$touser')";

        $result = mysql_query($sql);

         $Msg= "Success! <br/>";

    }
}
else{
    //Do nothing
}

//my code for the follow button

                        <form action="<?php $user;?>" method ="POST">
                        <?php echo $Msg; ?>
                        <input type = "submit" name ="addfriend" value = "Follow"/>
                        </form>
  • 写回答

2条回答 默认 最新

  • douyun3799 2015-02-24 15:58
    关注

    On the page where you are going to show the Follow or Unfollow button, first run a MySQL query to find out if you are already following the person:

    $sql = "select * from following 
            where userID = $user
            and   fromUser = $fromUser
            and   toUser = $toUser";
    $result = mysql_query($sql);
    
    if( $result) {
        if( mysql_num_rows($result) > 0) {
            // if we get here we know we are already following that person
            ....[see below]
    

    Now dynamically create whichever button you need:-

        if( mysql_num_rows($result) > 0) {
            // if we get here we know we are already following that person
            echo '<input type = "submit" name ="removefriend" value = "Un-follow"/>';
        }
        else
        {
            echo '<input type = "submit" name ="addfriend" value = "Follow"/>';
        }
    

    And on the following page where you are getting the form results, check for both buttons:

    if (isset($_POST['addfriend'])) {
        ...[do what you already have]
    }
    else
    if (isset($_POST['removefriend'])) {
        ...[do SQL to remove the record from the following table]
    }
    

    Please be aware also that as of PHP v5.5 this style of MySQL is deprecated. At some stage in the future you will have to convert your programs to the MySQLi or PDO_MySQL extensions, before they eventually discontinue support. See the PHP manual about this at eg http://php.net/manual/en/mysqlinfo.api.choosing.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择