Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 684089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:46:42+00:00 2026-05-14T01:46:42+00:00

My table is comment_likedislike. It has the comment_counterid, comment_counter, comment_id(which is from another table)

  • 0

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);
        }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:46:42+00:00Added an answer on May 14, 2026 at 1:46 am

    I’m trying to infer what you’re trying to do based on your code. It looks like you’re trying to increase a counter in a row corresponding to a given comment ID present in your GET parameters. And if there is no row corresponding to that comment you want to create a new one.

    If that’s what you’re intending to do then you’re going about it all wrong. It’s kind of difficult to explain how wrong your code is because I can’t figure out what mindset you’d have to be in to come up with code like that.

    First of all, you should put your check for the existence of the row in the SQL query, then you need to revise the structure of your if statements:

    if (isset($_GET['comment_id']) && isset($_GET['like_id'])) {
    
        // Ignoring obvious SQL injection vulnerability for now
        $query5="SELECT * FROM comment_likedislike WHERE comment_id = '" .
          $_GET['comment_id'] . "'";
    
        $data5=mysqli_query ($dbc, $query5);
        if ($row5= mysqli_fetch_array($data5)){
          $counter=$row5['comment_counter'];
          $counter++;
    
          $query= "UPDATE comment_likedislike SET comment_counter ='$counter' WHERE comment_id= '".$_GET['comment_id']."' ";
          mysqli_query($dbc, $query);
        }
        else 
          $counter2=1;
          $query9 = "INSERT INTO comment_likedislike (comment_counter, comment_id) VALUES ('$counter2', '".$_GET['comment_id']."' )";
          mysqli_query($dbc, $query9);
        }    
    }
    

    I’d also recommend reading Best way to stop SQL Injection in PHP because you never want to be building queries the way you’re doing it in your example. (Or mine, for that matter.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.