I am working on a like-dislike
process. I am using PHP for the backend. I have a table named comm_rating
in my database.
(NOTE:- Suppose there are 2 users Alex
and Siri
.)
When Alex
“likes” and Siri
“dislikes” the post, then Siri
see two “thumb-up” icons, and Alex
see two “thumb-up” and two “thumb-down” icons, which is wrong.
For this, I want the suggestion what I am doing wrong.
The following codes are as follow-
$numSqlCommLike = "SELECT * FROM `comm_rating` WHERE `com_rat_blog_id`='40' AND `com_rat_com_id`='59' AND `com_rat_action`='like'";
$numResultLike = mysqli_query($conn, $numSqlCommLike);
$numRowsLike = mysqli_num_rows($numResultLike);
if ($numRowsLike>0) {
echo '<button type="submit" class="like" name="button" value="like">';
$likeSqlChangeColor = "SELECT * FROM `comm_rating` WHERE `com_rat_user_id`='31' AND `com_rat_blog_id`='40' AND `com_rat_com_id`='59' AND `com_rat_action`='like'";
$likeResultChangeColor = mysqli_query($conn, $likeSqlChangeColor);
$likeRowsChangeColor = mysqli_num_rows($likeResultChangeColor);
if ($likeRowsChangeColor>0) {
echo '<i class="fa-solid fa-thumbs-up"></i>';
}else {
echo '<i class="fa-regular fa-thumbs-up"></i>';
}
echo '<p>'.$numRowsLike.'</p>
</button>';
$like_DislikeSqlChangeColor = "SELECT * FROM `comm_rating` WHERE `com_rat_user_id`='31' AND `com_rat_blog_id`='40' AND `com_rat_com_id`='59' AND `com_rat_action`='dislike'";
$like_dislikeResultChangeColor = mysqli_query($conn, $like_DislikeSqlChangeColor);
$like_dislikeRowsChangeColor = mysqli_num_rows($like_dislikeResultChangeColor);
if ($like_dislikeRowsChangeColor>0) {
echo '<button type="submit" class="dislike" name="button" value="dislike" style="display:none;"><i class="fa-regular fa-thumbs-down"></i><p>0</p></button>';
}else {
echo '<button type="submit" class="dislike" name="button" value="dislike" style="display:flex;"><i class="fa-regular fa-thumbs-down"></i><p>0</p></button>';
}
}
// Number of comment dislikes
$numSqlCommDislike = "SELECT * FROM `comm_rating` WHERE `com_rat_blog_id`='40' AND `com_rat_com_id`='59' AND `com_rat_action`='dislike'";
$numResultDislike = mysqli_query($conn, $numSqlCommDislike);
$numRowsDislike = mysqli_num_rows($numResultDislike);
if ($numRowsDislike>0) {
$dislike_LikeSqlChangeColor = "SELECT * FROM `comm_rating` WHERE `com_rat_user_id`='31' AND `com_rat_blog_id`='40' AND `com_rat_com_id`='59' AND `com_rat_action`='like'";
$dislike_LikeResultChangeColor = mysqli_query($conn, $dislike_LikeSqlChangeColor);
$dislike_LikeRowsChangeColor = mysqli_num_rows($dislike_LikeResultChangeColor);
if ($dislike_LikeRowsChangeColor>0) {
echo '<button type="submit" class="like" name="button" value="like" tyle="display:none;"><i class="fa-regular fa-thumbs-up"></i><p>0</p></button>';
}else {
echo '<button type="submit" class="like" name="button" value="like" tyle="display:flex;"><i class="fa-regular fa-thumbs-up"></i><p>0</p></button>';
}
echo '
<button type="submit" class="dislike" name="button" value="dislike">';
$dislikeSqlChangeColor = "SELECT * FROM `comm_rating` WHERE `com_rat_user_id`='31' AND `com_rat_blog_id`='40' AND `com_rat_com_id`='59' AND `com_rat_action`='dislike'";
$dislikeResultChangeColor = mysqli_query($conn, $dislikeSqlChangeColor);
$dislikeRowsChangeColor = mysqli_num_rows($dislikeResultChangeColor);
if ($dislikeRowsChangeColor>0) {
echo '<i class="fa-solid fa-thumbs-down"></i>';
}else {
echo '<i class="fa-regular fa-thumbs-down"></i>';
}
echo '<p>'.$numRowsDislike.'</p>
</button>
';
}
if ($numRowsLike===0 && $numRowsDislike===0) {
echo '
<button type="submit" class="like" name="button" value="like"><i class="fa-regular fa-thumbs-up"></i><p>0</p></button>
<button type="submit" class="dislike" name="button" value="dislike"><i class="fa-regular fa-thumbs-down"></i><p>0</p></button>
';
}