Thomas Skjørberg Thomas Skjørberg - 7 months ago 11
PHP Question

Trying to add a "like" function, no errors displayed, but wont work

I am trying to add a "like" function to my social network, however my code does not work and wont print any errors.
The functions uses $_GET to find out if a comment or a post should be "liked" and procedes to check if the user has already liked the post or comment. If so, the function will "unlike".

Can anyone help me figure out what I've done wrong?

Code:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
include "./db-connect.php";
$memberID= $_SESSION['memberID'];

if(isset($_GET['post_id'])){
$postID=$_GET['post_id'];
$sqlCheck="SELECT * from cs_likes WHERE post_id = $postID AND member_id = $memberID";
$sqlInsert="INSERT INTO cs_likes (post_id, member_id) VALUES ('$postID','$memberID')";
$sqlDelete="DELETE FROM cs_likes WHERE post_id= $postID AND member_id = $memberID";
}
elseif(isset($_GET['comment_id'])){
$commentID=$_GET['comment_id'];
$sqlCheck="SELECT * from cs_likes WHERE comment_id = $commentID AND member_id = $memberID";
$sqlInsert="INSERT INTO cs_likes (comment_id, member_id) VALUES ('$postID','$memberID')";
$sqlDelete="DELETE FROM cs_likes WHERE comment_id= $commentID AND member_id = $memberID";
}
$checkResult=mysqli_query($link, $sqlCheck);
if(mysqli_num_rows($checkResult)=0)
$result=mysqli_query($link,$sqlInsert);
else
$result=mysqli_query($link,$sqlDelete);



?>

Answer

Your code...

if(mysqli_num_rows($checkResult)=0)

Should be a double equal...

if(mysqli_num_rows($checkResult)==0)
Comments