paulious Posted November 30, 2009 Share Posted November 30, 2009 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 Link to comment https://forums.phpfreaks.com/topic/183459-php-voting-script-with-cookies-not-casting-vote/ Share on other sites More sharing options...
lemmin Posted November 30, 2009 Share Posted November 30, 2009 Where is $mode set? Try adding this to the top: if (isset($_GET['mode'])) $mode = $_GET['mode']; Link to comment https://forums.phpfreaks.com/topic/183459-php-voting-script-with-cookies-not-casting-vote/#findComment-968392 Share on other sites More sharing options...
paulious Posted November 30, 2009 Author Share Posted November 30, 2009 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); Link to comment https://forums.phpfreaks.com/topic/183459-php-voting-script-with-cookies-not-casting-vote/#findComment-968409 Share on other sites More sharing options...
lemmin Posted November 30, 2009 Share Posted November 30, 2009 That error is from a different page. I am guessing that you have a page that is including user_reviews.html? Link to comment https://forums.phpfreaks.com/topic/183459-php-voting-script-with-cookies-not-casting-vote/#findComment-968422 Share on other sites More sharing options...
paulious Posted November 30, 2009 Author Share Posted November 30, 2009 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. Link to comment https://forums.phpfreaks.com/topic/183459-php-voting-script-with-cookies-not-casting-vote/#findComment-968429 Share on other sites More sharing options...
lemmin Posted December 1, 2009 Share Posted December 1, 2009 Well, when you see an error like that it means that you are trying to send header information after already sending content. I'm guessing that the output from user_reviews.html is being send before a header() call in your PHP. Link to comment https://forums.phpfreaks.com/topic/183459-php-voting-script-with-cookies-not-casting-vote/#findComment-969131 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.