douzhang3822 2017-04-16 22:29
浏览 19

功能错误跟随/取消关注用户

hi am working on a script to allow users to follow each other but the action.php file to do the work is giving me a headache or maybe i don't know what and where i a have gone wrong i have 3 functions -check_count checks if the user is already following another user -follow_user executes the follow a user query -unfollow_user executes the unfollow user query

then, i have the action php file that gets ids from the follow and unfollow links

//home page
<div class="panel panel-default">
<div class="panel-body">
<?php
$users = show_users();
$following = following($_SESSION['login']);
if (count($users)){
foreach ($users as $key => $value){
echo $key," ", $value;
if(in_array($key, $following)){
echo " <small><a href='action.php?id=$key&do=unfollow'>unfollow</a>  </small>","<br>";
}else{
echo " <small><a href='action.php?id=$key&do=follow'>follow</a> </small>","<br>";
                                    }
                                }
                            }else{
                               echo "<p>","<b>","There are no users in the system","<b>","<p>"; 
                            }
                            ?>


                            </div>
                        </div>
//action .php file
<?php

session_start(); //session variables goes down here

include_once('includes/dbconnect.php'); include_once('functions.php');

$id = $_GET['id']; $do = $_GET['do'];

switch ($do){ case "follow": follow_user($_SESSION['login'],$id); $msg = "You have followed a user!"; break;

case "unfollow":
    unfollow_user($_SESSION['login'],$id);
    $msg = "You have unfollowed a user!";
break;

} $_SESSION['message'] = $msg;

header("Location:home.php"); ?>

 //the functions
 function check_count($first,$second){
    global $conn;
    $sql="SELECT COUNT(*) FROM following WHERE fuser_id='$second' AND follower_id='$first'";
    $result=mysqli_query($conn,$sql);
    $row = mysql_fetch_row($result);
    return $row[0];
}

function follow_user($me,$them){
    global $conn,$id;
    $count = check_count($me,$them);
        if($count==0){
            $sql="INSERT INTO following (fuser_id,follower_id) VALUES($them,$me)";
            $result=mysqli_query($conn,$sql);
        }

}

function unfollow_user($me,$them){
    global $conn,$id;
    $count = check_count($me,$them);
        if($count !=0){
            $sql="DELETE FROM following WHERE fuser_id='$them' and follower_id='$me' limit 1";
            $result=mysqli_query($conn,$sql);
        }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向