I'm working on like dislike rating system in PHP.
// if user clicks like or dislike button
if (isset($_POST['action'])) {
if (isset($_SESSION['uid'])) {
$action = $_POST['action'];
switch ($action) {
case 'like':
$sql = "INSERT INTO rating (uid, pid, action)
VALUES ($uid, $pid, 'like')
ON DUPLICATE KEY UPDATE action='like'";
break;
case 'dislike':
$sql = "INSERT INTO rating (uid, pid, action)
VALUES ($uid, $pid, 'dislike')
ON DUPLICATE KEY UPDATE action='dislike'";
break;
case 'unlike':
$sql = "DELETE FROM rating WHERE uid=$uid AND pid=$pid";
break;
case 'undislike':
$sql = "DELETE FROM rating WHERE uid=$uid AND pid=$pid";
break;
default:
break;
}
// execute query to effect changes in the database ...
mysqli_query($con, $sql);
echo getRating($pid);
exit(0);
} else {
header("location:login.php");
}
}
If a user is logged in then it's working fine but when user is not logged in then the else part which is including PHP header function is not working and not showing any error and not even inserting data into the database.
If there is another alternative then please do tell