您正在制作一个允许用户互相关注的脚本,但是action.php文件来完成这项工作让我很头疼或者我不知道哪里出错了我有3个功能 -check_count检查用户是否已经关注了另一个用户 -follow_user执行跟随用户查询 -unfollow_user执行取消关注用户查询
然后,我有动作php文件从后续和取消关注链接获取ID
//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(); //会话变量在这里下载
include_once(&#39;包括/ dbconnect.php&#39); include_once(&#39;的functions.php&#39);
$ id = $ _GET [&#39; id&#39;]; $ do = $ _GET [&#39; do&#39;];
开关($ do){ 案例&#34;关注&#34;: follow_user($ _ SESSION [&#39;登录&#39;],$ ID); $ msg =&#34;您关注了用户!&#34 ;; 打破;
case "unfollow":
unfollow_user($_SESSION['login'],$id);
$msg = "You have unfollowed a user!";
break;
} $ _SESSION [&#39; message&#39;] = $ msg;
头(&#34;位置:home.php&#34); ?&GT;
//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);
}
}