Can't seem to get my like/dislike to work in PHP
Posted
by user300371
on Stack Overflow
See other posts from Stack Overflow
or by user300371
Published on 2010-03-26T18:37:19Z
Indexed on
2010/03/26
18:43 UTC
Read the original article
Hit count: 151
php
My table is comment_likedislike. It has the comment_counterid, comment_counter, comment_id(which is from another table) fields. And I have an url (LIKE) that when clicked would link to this code and get the comment_id and like_id.
I want to do a count where if it is the first 'like', it would store a new comment_counter in the comment_likedislike table. But if there is already a 'like' for the comment in the table, it would just update the comment_counter to +1.
Problem: When I run this code, it doesn't UPDATE(1st statement) but INSERT(2nd if statement) no matter if there is a like for the comment or not. I don't think the code is checking if the comment_id is in the table already.
I am a novice php programmer.
Thanks!
if (isset($_GET['comment_id']) && isset($_GET['like_id'])) {
$query5="SELECT * FROM comment_likedislike ";
$data5=mysqli_query ($dbc, $query5);
while ($row5= mysqli_fetch_array($data5)){
$comment_id2=$row5['comment_id'];
}
if ($comment_id2 == $_GET['comment_id']){
$counter=$row5['comment_counter'];
$counter++;
$query= "UPDATE comment_likedislike SET comment_counter ='$counter' WHERE comment_id= '".$_GET['comment_id']."' ";
mysqli_query($dbc, $query);
}
if ($comment_id2 != $_GET['comment_id']) {
$counter2=1;
$query9 = "INSERT INTO comment_likedislike (comment_counter, comment_id) VALUES ('$counter2', '".$_GET['comment_id']."' )";
mysqli_query($dbc, $query9);
}
}
© Stack Overflow or respective owner