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);
}
}