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!
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!