Reputation: 27
I am using a rating script from http://net.tutsplus.com/tutorials/html-css-techniques/building-a-5-star-rating-system-with-jquery-ajax-and-php/
But I want it to upload the ratings to a database and stop a person from voting the same picture all the time.
This is my script to upload and set a cookie:
<?php
// Get id and voted value
$id = $_POST['widget_id'];
preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
$vote = $match[1];
// Connect to database and find the row which have the id
$get = mysql_query("SELECT * FROM ratings WHERE id = '$id'");
while ($getdata = mysql_fetch_array($get)) {
$total_votes = $getdata['total_votes'];
$total_value = $getdata['total_value'];
// See if the votes and value is 0 and if it aint it update the database and if it is were creating a new row
if ($total_votes != 0 && $total_value != 0) {
$total_votes++;
mysql_query("UPDATE ratings SET total_votes = '$total_votes' WHERE
id = '$id'");
} else {
mysql_query("INSERT INTO ratings (id, total_votes, total_value)
VALUES ('$id', '1', '$vote')");
}
// Sets the cookie
$value = $id;
// Send a cookie that expires in 24 hours
setcookie($id, $value, time() + 3600 * 24);
?>
But if the user already has voted he can still vote so I need someway to check if he has the cookie and a way to get the data from the mysql table and send it to the user.
Upvotes: 0
Views: 205
Reputation: 27
Ok i got the function to check if the cookie is set done so now i only need to get the data from the mysql table and output to the javascript
Here are my code
<?php
// Get id and voted value
$id = $_POST['widget_id'];
preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
$vote = $match[1];
// Connect to database and find the row which have the id
$get = mysql_query("SELECT * FROM ratings WHERE id = '$id'");
if (isset($_COOKIE[$id])) {
echo 'cookie exist';
exit;
} else {
while ($getdata = mysql_fetch_array($get)) {
$total_votes = $getdata['total_votes'];
$total_value = $getdata['total_value'];
// See if the votes and value is 0 and if it aint it update the database and if it is were creating a new row
if ($total_votes != 0 && $total_value != 0) {
$total_votes++;
mysql_query("UPDATE ratings SET total_votes = '$total_votes' WHERE
id = '$id'");
} else {
mysql_query("INSERT INTO ratings (id, total_votes, total_value)
VALUES ('$id', '1', '$vote')");
}
// Send a cookie that expires in 24 hours
setcookie($id, $id, time() + 3600 * 24);
}
?>
Upvotes: 0
Reputation: 2597
Here you set $value to be the same as id.
$value = $id;
And here you make a cookie called the value of the variable $id which makes a dynamic name of the cookie.
setcookie($id,$value, time()+3600*24);
To make a cookie always set a static name
//create cookie
setcookie('widget_id',$id, time()+3600*24); // $value is useless
//read cookie
echo $_COOKIE['widget_id']; //prints the cookie
// unset cookie
unset($_COOKIE['widget_id'];);
Upvotes: 1
Reputation: 2998
You shouldn't keep track of whether or not a user has voted on a particular thing with a cookie.
Instead, you should check your database to make sure the user hasn't voted on that particular thing before submitting the INSERT vote query.
If the user has already voted, perhaps perform an UPDATE instead of an INSERT to change their vote to the new value.
So you would be checking like this:
SELECT * FROM ratings
WHERE userid=:userid
You will benefit from using PDO (http://php.net/manual/en/book.pdo.php) to bind necessary parameters and can then easily check how many rows are returned (http://php.net/manual/en/pdostatement.rowcount.php).
If 1 row is returned, the user has already voted. Then you can update the vote with the new vote value.
If 0 rows are returned, the user has not already voted. Then you can do the insert like you do in your code above.
If more than 1 row is returned, then multiple votes have been inserted already and you need to delete all but 1 of those votes.
I suggest adding a column for the individual pages / objects so you can use this functionality.
Upvotes: -1
Reputation: 1239
Incorrect syntax...
$total_votes = $total_votes . +1;
This will add 1 to $total_votes
$total_votes++;
Upvotes: 2