Jump to content

PHP voting script with cookies not casting vote


paulious

Recommended Posts

Hi

I am trying to get a basic voting script to work, most of it was sourced from http://php.about.com/od/finishedphp1/ss/rating_script_2.htm but it doesn't seem to work

 

I have set up mySQL Database and table, checked in PHPmyAdmin and they are fine. When i run the script it displays the results which i manually entered into the database via PHPmyAdmin but when i click on a link to vote all it does is refresh the page and not cast the vote.

 


    <?php 
// Connects to your Database 
mysql_connect("localhost", "paulious_paul", "paul") or die(mysql_error()); 
mysql_select_db("paulious_vote") or die(mysql_error()); 


//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 
"<br>";
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> | ";
"<br>"; 
}
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote") 
{ 


$cookie = "Mysite$id"; 
if(isset($_COOKIE[$cookie])) 
{ 
Echo "Sorry You have already ranked that site <p>"; 
} 


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 paulious_vote SET total = total+$voted, votes = votes+1 WHERE id = $id"); 
Echo "Your vote has been cast <p>"; 
} 
} 

?>

 

Thanks for looking

Thank you for the very prompt reply.

 

I added the lines as you suggested just after connection to DB is established and i receive this error

 

"Warning: Cannot modify header information - headers already sent by (output started at /home/paulious/public_html/reviews/user_reviews.html:9) in /home/paulious/public_html/reviews/user_reviews.html on line 168

Your vote has been cast "

 

and it doesn't appear to update the database

 

Edit - line 168 is

  setcookie(Mysite.$id, Voted, $month);

erm not really, I did hash together the references to images and CSS on that page as it is not in the root so i specified them by the complete URL. Other than that i don't think user_reviews.html is included anywhere except a reference to it in the Spry Menubar.  I appreciate you taking the time. The web link for the page is www.reviewdatabase.200u.com then on the menu click reviews then dropdown - user reviews. That page is where the PHP code is placed.  I'm abit tired of it for tonight so going to look at it with a fresh head tomorrow.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.