I have a cookbook program that I'm trying to get going. It was written for globals on and of course we are rewriting it since we have globals off and plan to keep it that way.
I'm having a problem with ratings for the recipes. What is happening is everytime I rate to test it, it ups the votes in the array for next time....I want it to go by the votes in the db. What am I doing wrong?
Form variables are passed via post and all is correct there.
Here is the calculation:
if($dowhat == "rate")
{
$query="SELECT * FROM votes WHERE ids='$id'";
$result = mysql_db_query($db, $query);
$rows = mysql_num_rows($result);
if($rows == 0)
{ //2
$query = "INSERT into votes (ids, votes, points, average, title, id )values('$id', '1', '$rating', '$rating', '$title','')";
$result = mysql_db_query($db, $query);
echo mysql_errno().": ".mysql_error()."<BR>"; // uncomment to troubleshoot db problems
$average = $rating;
//echo mysql_errno().": ".mysql_error()."<BR>"; // uncomment to troubleshoot db problems
}//2
else
{ //3
$r = mysql_fetch_array($result);
print $r;
$votes = $r["votes"];
$points = $r["points"];
$points = $points + $rating;
$votes = $votes + 1;
$average = $points / $votes;
$average = round($average, 1);
$query = "UPDATE votes SET votes = '$votes', points = '$points', average = '$average' where ids = '$id'";
$result2 = mysql_db_query($db, $query);
// echo mysql_errno().": ".mysql_error()."<BR>"; // uncomment to troubleshoot db problems
echo $votes;
echo $points;
echo $average;
echo $rating;
} //3
if($result || $result2)
{ //4 print form
}
To test this code go to:
view recipes
Click on Rate this Recipe and you are taken to rate.php where all the above code resides. Then you are asked to rate it and it should add to the db and return a correct average....neither is happening. Every time I test this it adds a vote in the array but it is not in the db...the recipe has 7 points and 1 vote. I've pulled the calculation of average out and posted my variables and tested and it works but once you throw the array back in it doesn't work right. I haven't provided the entire page. Let me know if I need to. Also, I have a print and few echos in there that are just so I can see the values. They will be removed once this works. TIA!!!