<?php
session_start();

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");


if(isset($_POST['user_id']))
{
require 'config.php';


function check_input($par) {
  $par = trim($par);
  $par = stripslashes($par);
  $par = htmlspecialchars($par);
  return $par;
}



$auth=check_input($_POST['auth']);
$user_id=check_input($_POST['user_id']);
$following=check_input($_POST['following']);


$auth = mysqli_escape_string($con, $auth);
$user_id=mysqli_escape_string($con, $user_id);
$post_id=mysqli_escape_string($con, $post_id);
$following=mysqli_escape_string($con, $following);




$sql="SELECT * FROM users WHERE user_id = '$user_id' AND auth = '$auth'";
$result=$con->query($sql) or die ("error: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);

if ($count == 1)
{
$rows = mysqli_fetch_array($result);
$phone = $rows['phone'];
$email = $rows['email'];
$firstname = $rows['firstname'];
$lastname = $rows['lastname'];

date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y h:i:s a"); 
  
 
 

$sql2="SELECT * FROM followings WHERE user_id = '$user_id' AND  following = '$following'";
$result2=$con->query($sql2) or die ("error: Server error ".mysqli_error($con));
$count2=mysqli_num_rows($result2);

if ($count2 == 0)
{
$rows2 = mysqli_fetch_array($result2);
$likes = $rows2['likes'];
$likes = $likes+1;
$notify = $firstname." ".$lastname." Liked your post.";


$sqlx=$con->query("INSERT INTO followings  (user_id, user, following,  date_t) 
      VALUES ('$user_id', '$email', '$following',  '$date_t')") or die("Error: ".mysqli_error($con));
      



if($sqlx)
{
$suc = "Successful"; 
$status = true; 
}

}
else
{
$suc = "Alredy following this account"; 
$status = false; 
}


}
else
{
$suc = "User Does Not Exist"; 
$status = false; 
}

$con->close();

$output = array('status' => $status, 'success'=>$suc);
echo json_encode($output); //output JSON data
}
?>
