Jump to content

5 Star Voting Script Help...Update Query Runs Twice in a Row.


socalnate

Recommended Posts

For some reason this query below runs twice in a row when I don't want it to.  Basically when someone votes it doubles the vote and doubles the value.  Can't figure out why...Please help!  :-\

 

mysql_query("UPDATE $tableName SET total_votes='".$added."', total_value='".$sum."', used_ips='".$insert."' WHERE id='".$_GET['id']."'");

 

 

<?php
$tableName="ratings";
$rating_posted=$_GET['vote'];//pased variable by the the stars value...IS NOT the vote, but the rating of the vote.
$id=$_GET['id'];
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='".$id."' ")or die(" Error: ".mysql_error());
$numbers=mysql_fetch_assoc($query);
$checkIP=unserialize($numbers['used_ips']);
$count=$numbers['total_votes'];//how many votes total
$current_rating=$numbers['total_value'];//total number of rating added together and stored
$sum=$rating_posted+$current_rating;// add together the current vote value and the total vote value
// $five_star_score = round($current_rating/$count, 2); // can't divides here because you'll get a divide by zero error
// $percent =  ($five_star_score/5) * 100; //
$tense=($count==1) ? "vote" : "votes";//plural form votes/vote
$voted=@mysql_fetch_assoc(@mysql_query("SELECT * FROM $tableName WHERE used_ips LIKE '%".$_SERVER['REMOTE_ADDR']."%' AND id='$id' ")); //Pattern match ip:suggested by Bramus! http://www.bram.us/ - this variable searches through the previous ip address that have voted and returns true or false

if($voted){

echo "<div class=\"rating\">".
"<ul class=\"star-rating\">".
"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*25 ."px;\">Current rating.</li>".
     "<li class=\"one-star\"></li>".
     "<li class=\"two-stars\" ></li>".
     "<li class=\"three-stars\"></li>".
     "<li class=\"four-stars\"></li>".
     "<li class=\"five-stars\"></li>".
"</ul>".
"<p>Rating: <strong>".@number_format($current_rating/$count,2)."</strong> {".$count." ".$tense." cast} <br />You have already voted.</p></div>";//show the current value of the vote with the current numbers
	}
// IP NOT LOGGED AND THEREFORE NOT VOTED 
else {
	if(isset($_GET['vote'])) { // NOTE: Returns boolean yes/or no IF the "vote" has a value.  NOT the vote word itself, but the value of "vote"
		if($sum==0){ // FILTER DESCRIPTION: Do Not increase number of votes if there is 1) No vote value posted AND 2) The is no current rating. 
			 	     // SCENARIO: Someone enters a 0 vote in the toolbar AND there is no total_value in the server  
				$added=0; // IS THE SAME AS 0  
				        }
		else { // FILTER DESCRIPTION: If there IS value to the vote
			$added=$count+1;// Increment the current total of votes.   
			 }
		if(is_array($checkIP)){ //Finds whether the given variable is an array; there could be multiple IP's in that column.  
			array_push($checkIP,$_SERVER['REMOTE_ADDR']);  // If it is an array, it pushes the IP of the current user to the bottom of the stack.
							}
		else{ // Not an array for the first entry
			$checkIP=array($_SERVER['REMOTE_ADDR']);
			}
		$insert=serialize($checkIP); // Generates a storable representation of a value.  .
		echo "<p>Total Votes Value: $added</p>";
		echo "<p>Total Value: $sum</p>";
		mysql_query("UPDATE $tableName SET total_votes='".$added."', total_value='".$sum."', used_ips='".$insert."' WHERE id='".$_GET['id']."'");



		echo "<div class=\"rating\"><p>Rating: <strong>".@number_format($sum/$added,2)."</strong> {".$added." ".$tense." cast} <span><br />Thank you for your vote!</span></p></div>";//show the updated value of the vote
		 	 } // END TRUE ISSET, NEXT TO FALSE:
	else { // If the 'vote' value has NOT been set!
?>				
			<div class="rating">
			<ul class="star-rating">
			<li class="current-rating" style="width:<?php echo @number_format($current_rating/$count,2)*25 ?>px;">Current rating</li>
    			<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id'] . "&vote=1";?>" title="Rate this 1 star out of 5" class="one-star">1</a></li>
     			<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id'] . "&vote=2";?>" title="Rate this 2 stars out of 5" class="two-stars" >2</a></li>
     			<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id'] . "&vote=3";?>" title="Rate this 3 stars out of 5" class="three-stars" >3</a></li>
     			<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id'] . "&vote=4";?>" title="Rate this 4 stars out of 5" class="four-stars" >4</a></li>
     			<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?id=" . $_GET['id'] . "&vote=5";?>" title="Rate this 5 stars out of 5" class="five-stars" >5</a></li>
			</ul>
<?php
echo	"<p>Rating: <strong>".@number_format($sum/$count,2)."</strong> {".$count." ".$tense." cast}</p></div>";//show the current updated value of the vote
		  }	// end isset get vote	
	}	//end voted true, false
?>

Link to comment
Share on other sites

Nothing in your code indicates ANY form of looping.  There are no defined functions, there isn't a for, foreach, or while loop, so if your code is executing multiple times, it isn't because of that piece of script.  May I ask why you believe it is executing twice?  What gives you that impression.

Link to comment
Share on other sites

There is NO need to double repost.  I only want one vote per one rating...but whenever someone votes it doubles the vote count and the rating count.  Troubleshooting this I echoed the variables right before being "UPDATED" to my MySQL server to see what they read.  Here's the code for that:

 

$insert=serialize($checkIP); // Generates a storable representation of a value.  .
		echo "<p>Total Votes Value: $added</p>";
		echo "<p>Total Value: $sum</p>";
		mysql_query("UPDATE $tableName SET total_votes='".$added."', total_value='".$sum."', used_ips='".$insert."' WHERE id='".$_GET['id']."'");

 

 

What's even MORE bizzare is that those variables were absolutely correct right before being updated;  The $added and $sum were perfect.  And also the code below it echoing the new Rating and Vote count were also correct.

 

echo "<div class=\"rating\"><p>Rating: <strong>".@number_format($sum/$added,2)."</strong> {".$added." ".$tense." cast} <span><br />Thank you for your vote!</span></p></div>";//show the updated value of the vote
		 	 } // END TRUE ISSET, NEXT TO FALSE:

 

 

However, here's the killer:  When I looked in my database to check the vote count and value count everything was doubled as if someone had voted twice!  IE.  A 1 vote with a 4 points rating became 2 votes and 8 points.  So something is causing both values to be doubled...could there be something in my CSS, HTML code that is causing this?  Here is some additional code that runs ABOVE the ratings system...wondering if there's a conflict there? 

 

<?
$gameid=$_GET['id'];

// sql statement to grab the descriptions.  
$sql1 = mysql_query("SELECT gamesnew .*, descriptions . description , genres . genre FROM gamesnew LEFT JOIN descriptions ON gamesnew . game_name =descriptions . game_name LEFT JOIN genres ON descriptions . game_name =genres . game_name  WHERE gamesnew.id = '$gameid'"); 

if (!$sql1) {
   echo 'Could not run query $slq1: ' . mysql_error();
   exit;
}
$row1 = mysql_fetch_array($sql1); 
$cr_datetime = $row1['cr_datetime'];
$sort_date = $row1['sort_date'];
$game_name = $row1['game_name'];
$description = $row1['description'];
if (!$description)
{
$description = "There is currently no description ...";
}
$two_game_genres=$row1['genre'];
list($game_genre_one, $game_genre_two) = split('[ & ]', $two_game_genres);

$game_genre_cap=ucfirst($game_genre_one);
$thumb=$row1['thumb'];
$flashfile = $row1['flash_file'];

// This query only grabs the votes and the scores from the ID given. 

$sql2 = mysql_query("SELECT ratings . id , ratings . total_votes , ratings . total_value FROM gamesnew LEFT JOIN ratings ON gamesnew . game_name =ratings . game_name WHERE gamesnew.id = '$gameid'"); 
if (!$sql2) {
   echo 'Could not run query $slq2: ' . mysql_error();
   exit;
}
// This is to format the width of the popup.
$flashsize = getimagesize("http://website.com/games/$flashfile");
$height = ($flashsize[1] + 100);
$width = $flashsize[0];
if ($width <= 300)
         {
               $width = 300;
         }
?>

 

   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.