PDA

View Full Version : One vote per user


idunnopoetry
04-25-2003, 09:27 PM
I have written a script to allow users to rate poems. The only problem with this is making it so that each user can only rate each poem once. So far, I've been able to get it to work partially; users can only rate a poem once, but once they rate a different poem they can come back and rate the other one again. I would like to have it to where each person can only rate each poem once, without having to register or sign up at the site. I am not sure which would be best, using sessions or cookies... I would like it to be persistent, so I am guessing cookies would be best, but I can't seem to find any decent documentation or tutorials on utilizing them for my purpose. If someone could help me out with this little problem, it would be greatly appreciated. For reference, here is my code:


if ($submitrate == "RATE")
{
if ($voted == $poemid)
{
echo( "already voted!" );
exit();
}

else
{
$voted = $poemid;
session_register(voted);
}

$query="SELECT * from Rates where RateID='$poemid'";
$result= mysql_db_query(idunnopoetry, $query, $dbcnx) or die
("Could not execute query : $query." . mysql_error());

while ($row=mysql_fetch_array($result))
{
$RateID=$row["RateID"];
$PoemID=$row["PoemID"];
$TotalCount=$row["TotalCount"];
$TotalSum=$row["TotalSum"];

$TotalSum = $TotalSum + $rating;

$TotalCount = ++$TotalCount;

$NewAvg = $TotalSum / $TotalCount;

$NewAvg = round($NewAvg,2);

$newquery = "UPDATE Rates set AvgRate = '$NewAvg', TotalSum='$TotalSum', TotalCount='$TotalCount'
where RateID='$poemid'";

$newresult = mysql_query($newquery);

if ($newresult)
{
echo "Thank you, Poem has been rated.";
}

}
}


Thanks!

Mhtml
04-25-2003, 10:03 PM
Are the users registered users?

idunnopoetry
04-25-2003, 10:11 PM
I would like to have it to where each person can only rate each poem once, without having to register or sign up at the site.

:)

Mhtml
04-25-2003, 10:26 PM
Oops missed that, sorry.
I guess you are using cookies... so insert each new poem id into the cookie separated by a | or something and then when calling it split it up into an array.

idunnopoetry
04-25-2003, 10:28 PM
Well, that's my problem, I don't know how. :(

idunnopoetry
04-27-2003, 03:14 AM
Does anyone at least know of a good site or tutorial on sessions/cookies that would explain what I need to know? I've been looking, but I can't seem to find one.

Nightfire
04-27-2003, 03:21 AM
The best you can get... http://php.net/setcookie :) Example1 should help you see things clearly