im trying to make a rating system where the user can either "like" or "dislike" a picture. but for testing im just using 2 names from a mysql database. once the user hits like, the query will add 1 to the column "name_votes", and would subtract 1 if the user hits dislike.i need help on matching the button to the name, so when the user hits like, it will add one to that name. also, when i hit like, it doesn't add 1 to the database. i need help with that.
-i have not done the dislike code yet, i have just done the like code.


$con = mysql_connect("localhost","root","");
function like(){

$like = "UPDATE \"testrating\" SET \"name_votes\"=\"name_votes\" +1 WHERE \"id\" = \"button\" ";

$selection = "SELECT * FROM testrating";

$result = mysql_query($selection);

while($row = mysql_fetch_array($result))

echo $row["name"]."<form method=\"post\" action=\"\"> <input type=\"button\" id = \"button++\" name = \"crave\" value=\"Crave\" </form></br>";




am also looking for this script any help will be greatly appreciated

Member Avatar

You may need a more robust setup if you want to disallow voting more than once on an item. Maybe:

item_id (FK to items) | vote_value (1 or -1) | voter_id (FK to users table)

You can calculate the value of the total votes with SUM(vote_value)
Voters can be denied the right to vote again OR, better still be shown their cast vote with the option of changing it, so if +1, they are shown:

You like this [Remove Vote][Dislike]

Or similar.

I have made small demo for you. Play around it to fit your needs.

CREATE TABLE `likesys` (
  `liked` INT(1)  NOT NULL,
  PRIMARY KEY (`id`)

PHP file


    ini_set("display_errors", 1);//error mgt
        $db = new PDO("mysql:host=localhost;dbname=test", "test_user", "__put_password_here__");
    }catch(PDOException $e){        
        echo $e->getMessage();
    $stmt=$db->prepare("INSERT INTO likesys(liked) VALUES(:like)");
    $stmt2=$db->prepare("SELECT SUM(liked) AS likes FROM likesys WHERE liked=:like");
        $res = $stmt2->fetch(PDO::FETCH_ASSOC);  
        echo "<p>Likes: ". $res['likes']."</p>";
        $res = $stmt2->fetch(PDO::FETCH_ASSOC);  
        echo "<p>Dislikes: ". abs($res['likes'])."</p>";
   // }
    <form action=<?php echo $_SERVER["PHP_SELF"];?> method="POST" >
        <input name="like" type="submit" value="Like" />
        <input name="dislike" type="submit" value="Dislike" />

The best way would be wrapping these into class and do it in two line:

$liker = new LikerClass(true);//true for like(default) and false for dislike
$liker->action();//do like/unlike

thanks, alot, i will try to study the code to help my rating system