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 8880917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:14:49+00:00 2026-06-14T20:14:49+00:00

I have a comics website where I’m trying to implement a like/dislike system. Each

  • 0

I have a comics website where I’m trying to implement a like/dislike system. Each user can only vote once on a particular comic. The comics are stored in ‘comics’ table, artwork stored in ‘artwork’, and I have a ‘votes’ table with columns (ip, table_name, imgid).

When someone votes, I want to store their IP against that image id and table within “votes” table. If they try to vote again, it will check that table to see if they have voted.

Also, I want to do a ON DUPLICATE KEY UPDATE which will update the Primary Key “ip” in the votes table if someone with that IP tries to vote again.

include 'dbconnect.php';
$site = $_GET['_site'];
$imgid = intval($_GET['_id']);
$input = $_GET['_choice'];


if ($site == "artwork") {
$table = "artwork";
}
else {
$table = "comics";
}

$result = $mysqli->query("SELECT like_count, dislike_count FROM $table WHERE id = $imgid");

list($likes, $dislikes) = $result->fetch_array(MYSQLI_NUM);

$sql = "INSERT INTO 
            votes (ip, table_name, imgid) 
        VALUES 
            (\"".$_SERVER['REMOTE_ADDR']."\", \"$table\", $imgid)
        ON DUPLICATE KEY UPDATE
            ip = VALUES(ip),
            table_name = VALUES(table_name),
            imgid = VALUES(imgid)";


if (!$mysqli->query($sql)) printf("Error: %s\n", $mysqli->error); 

$sql = "SELECT ip FROM votes WHERE ip = '".$_SERVER['REMOTE_ADDR']."' AND table_name = '$table' AND imgid = $imgid";

if ($result = $mysqli->query($sql)) {

    if ($result->num_rows == 0) { 
        if ($input == "like") {
            $sql = "UPDATE $table SET like_count = like_count + 1 WHERE id = $imgid";
            $mysqli->query($sql);           
            $likes++;
        }
        else if ($input == "dislike") {
            $sql = "UPDATE $table SET dislike_count = dislike_count + 1 WHERE id = $imgid";
            $mysqli->query($sql);
            $dislikes++;        
        }
    echo "Likes: " . $likes . ", Dislikes: " . $dislikes;
    }
    else {
        echo "You have already voted";
    }
}
else { 
    printf("Error: %s\n", $mysqli->error); 
}
mysqli_close($mysqli);

Any thoughts?

  • 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-06-14T20:14:50+00:00Added an answer on June 14, 2026 at 8:14 pm

    table is a reserved word in MySQL. If you want to use it, you have to enclose it in backticks. In your case, however, I think that the you meant to use table_name instead:

    $sql = "INSERT INTO 
                votes (`ip`, `table_name`, `imgid`) 
            VALUES 
                (\"".$_SERVER['REMOTE_ADDR']."\", \"$table\", $imgid)
            ON DUPLICATE KEY UPDATE
               `ip` = VALUES(`ip`),
               `table_name` = VALUES(`table_name`),
               `imgid` = VALUES(`imgid`)";
    

    From the syntax of your query, you should consider using REPLACE:

    REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

    So your query would resolve to this:

    $sql = "REPLACE INTO 
                votes (`ip`, `table_name`, `imgid`) 
            VALUES 
                (\"".$_SERVER['REMOTE_ADDR']."\", \"$table\", $imgid)";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comics website where I'd like to allow users to vote once
I'm building a website which will show comics. I'd like to store each image
I'm building a website in PHP to share my comics. I'd like to implement
I have a comics website, and I'd like to keep track of how many
I have a comics website which loops through all images in a db and
I have a simple application that views comics and allows the user to mark
Comics object can have many Chapter object. I have this in Comics class: @OneToMany(targetEntity=Chapter.class,
I am building a comics website and have implemented a search. The search originally
I am making a comics website... it will have two sub sites: Comics and
I have a comics website which loops through all images in a db and

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.