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.

<?php

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

$like = "UPDATE \"testrating\" SET \"name_votes\"=\"name_votes\" +1 WHERE \"id\" = \"button\" ";
mysql_query($like);
}






$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>";

}
if(isset($_POST['crave']))
{

like();


}

Recommended Answers

All 6 Replies

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

Member Avatar for diafol

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

votes
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.
Schema:

CREATE TABLE `likesys` (
  `id` INT  NOT NULL AUTO_INCREMENT,
  `liked` INT(1)  NOT NULL,
  PRIMARY KEY (`id`)
)
ENGINE = MyISAM;

PHP file

<html>
<head>

</head>
<body>
<?php
    ini_set("display_errors", 1);//error mgt
    try{
        $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");
    $display=false;
    if(isset($_POST["like"])){
        $stmt->execute(array(":like"=>"1")); 
        $display=true;
    }
    if(isset($_POST["dislike"])){
        $stmt->execute(array(":like"=>"-1")); 
        $display=true;
    }
    $stmt->closeCursor();
    
    //if($display){ 
        $stmt2->execute(array(":like"=>"1"));
        $res = $stmt2->fetch(PDO::FETCH_ASSOC);  
        echo "<p>Likes: ". $res['likes']."</p>";
        
        $stmt2->execute(array(":like"=>"-1"));
        $res = $stmt2->fetch(PDO::FETCH_ASSOC);  
        echo "<p>Dislikes: ". abs($res['likes'])."</p>";
   // }
    
    $db=null;
?>
    <form action=<?php echo $_SERVER["PHP_SELF"];?> method="POST" >
        <input name="like" type="submit" value="Like" />
        <input name="dislike" type="submit" value="Dislike" />
    </form>
</body>
</html>

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
$liker->getLikes();
$liker->getDislikes();

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

don't forget to mark it solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.