0

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


}

Edited by Mayank23: n/a

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Stefano Mtangoo
0

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.

Edited by diafol: n/a

0

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>

Edited by Stefano Mtangoo: n/a

0

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

Edited by Stefano Mtangoo: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.