So I've been browsing the internet looking on ways to create a webpage for rating images (somewhat like hot or not, but not in the same ballpark, it'll be far more simple, no users or comments or anything for the sort). Anyways, I've stumbled across a little tutorial at about.com on how to create a VERY basic rating script (just so I can understand how this should work). However, following the tutorial verbatim (making sure the MySQL DB information is correct and connectible), their code seems to have flaws and doesn't preform MySQL updates correctly. I've been trying all these little edits to correct the problem, but I can't seem to get it to work.
I've omitted my database information.
<?php
// Connects to your Database
mysql_connect("xxxxx", "xxxxx", "xxxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxxxxx") or die(mysql_error());
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{
//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}
//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month);
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
}
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM vote") or die(mysql_error());
//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
{
//This outputs the sites name
Echo "Name: " .$ratings['name']."<br>";
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
$current = $ratings[total] / $ratings[votes];
Echo "Current Rating: " . round($current, 1) . "<br>";
//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
Echo "Rank Me: ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">Vote 1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">Vote 2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">Vote 3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">Vote 4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">Vote 5</a><p>";
}
?>
It seems to me the error lies in the if ( $mode=="vote") area. The .$ratings[id] are correct for each subject being voted it, but it seems that the script isn't catching links like http://www.mywebsite.com/vote.php?mode=vote&voted=5&id=1 (I can also tell because it doesn't even add that cookie when I vote, and it doesn't echo/print anything if I try to error test the if statement.)
I have plans to add onto this script, which I have successfully when tinkering with it, but it's all worthless unless I can get this vote script up and running. I know this script is VERY basic (it'll need an if statement to avoid dividing by zero, and one to keep people from exploiting the system, etc. etc. but those I won't have a problem doing myself)
I'm just very confused on why it won't update the MySQL rows for total and votes, it reads from the DB alright and lists all the items to be voted on. More information on this tutorial is here: http://php.about.com/od/finishedphp1/ss/rating_script.htm
Any help would be greatly appreciated.