I have been trying to solve this for the last week so please please let me know what is causing this… My page randomly generates a photo from my database. When a user rates the photo, the page randomly generates a new photo from the database, however, as soon as the user rates the photo, the page seems to generate a photo twice over…. It loads a new image for .01 of a second, then a new images replaces it again and stays there until the user rates it…. and the same thing happens again each time a user rates a photo…. [COLOR=”RoyalBlue”]THANKS IN ADVANCE
Here is my code below:
[code=php]<?php
session_start();
//rate.php
$dbh = mysql_connect(“localhost”, “root”, “”) or die(mysql_error());
mysql_select_db(“sample”) or die(mysql_error());
?>
<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 3.2 Final//EN’>
<html>
<head>
<title>Rate People</title>
<meta http-equiv=’Content-Type’ content=’text/html; charset=utf-8′>
<link rel=stylesheet href=stylesheet.css type=text/css>
</head>
<body>
<div align=center>
<table width=”760″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td width=”145″><a href=”index.php” target=”_self”><img src=”Images/lt.gif” width=”145″ height=”58″ border=”0″></a></td>
<td width=”145″><a href=”index.php” target=”_self”><img src=”Images/rt.gif” width=”145″ height=”58″ border=”0″></a></td>
<td align=”right” width=”470″> <table width=465 border=0 cellspacing=0 cellpadding=0 class=”table_header”>
<tr>
<td width=2><img src=Images/status_bar_l.gif width=2 height=27></td>
<td align=right background=Images/status_bar_background.gif><a href=”sign.php” target=”_self” class=”linksHeader”>Sign
In</a></td>
<td width=10 align=right background=Images/status_bar_background.gif><img src=Images/arrow.gif width=10 height=27 hspace=5></td>
<td width=3 align=right><img src=Images/status_bar_background.gif width=3 height=27></td>
<td width=2><img src=Images/status_bar_r.gif width=2 height=27></td>
</tr>
</table></td>
</tr>
<tr>
<td height=”58″><a href=”index.php” target=”_self”><img src=”Images/lb.gif” width=”145″ height=”58″ border=”0″></a></td>
<td colspan=”2″><div align=”right”><a href=”index.php” target=”_self”><img src=”Images/nav/home_white.gif” width=”79″ height=”42″ border=”0″></a><img src=”Images/nav/rate_people_blue.gif” width=”105″ height=”42″ border=”0″><a href=”meet.php” target=”_self”><img src=”Images/nav/meet_people_white.gif” width=”105″ height=”42″ border=”0″></a><a href=”counties.php” target=”_self”><img src=”Images/nav/view_counties_white.gif” width=”118″ height=”42″ border=”0″></a><a href=”join.form.php” target=”_self”><img src=”Images/nav/submit_photo_white.gif” width=”117″ height=”42″ border=”0″></a><a href=”help.php” target=”_self”><img src=”Images/nav/help_white.gif” width=”69″ height=”42″ border=”0″></a></div></td>
</tr>
</table>
<!–<table width=760 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=”12″> </td>
</tr>
</table>–>
[code=php]<?php
if (isset($_SESSION[‘previousid’]))
{//print “first”;
$id = $_SESSION[‘previousid’];
$sqllastrating = “SELECT average_score, votes FROM accounts where id= $id”; // select 1 random record from accounts
$previousresult = mysql_query($sqllastrating);
$previouslist = mysql_fetch_array($previousresult);
$votedaverage = $previouslist[‘average_score’];
$votedvotes = $previouslist[‘votes’];
session_destroy();
}
$sql = “SELECT * FROM accounts ORDER BY RAND() LIMIT 1”; // select 1 random record from accounts
$result = mysql_query($sql);
$nrows = mysql_num_rows($result);
if ($nrows != 0) {
$list = mysql_fetch_array($result);
$id_to_rate = $list[‘id’];
$current_score = $list[‘average_score’];
$current_votes = $list[‘votes’];
?>
<table width=760 border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=259 height=”520″ vAlign=top bgcolor=”#FFFFFF”><table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td height=”117″ colspan=”2″ class=”rate_top”><table width=”100%” height=”106″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td width=”85%”> </td>
<td width=”15%”> </td>
</tr>
<tr>
<td height=”54″ class=”average”>
<?php print $votedaverage;?>
</td>
<!– average –>
<td> </td>
</tr>
</table></td>
</tr
><tr>
<td width=”33%” height=”62″ class=”rate_bottom_left”> </td>
<td width=”67%” height=”62″ class=”rate_bottom_right”><table width=”100%” height=”78″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td width=”49%” height=”52″ class=”votes”>
<?php print $votedvotes;?>
</td>
<!– votes –>
<td width=”51%”> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
<br></td>
<td width=501 vAlign=top bgcolor=”#FFFFFF”>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td>
<table align=center cellpadding=0 cellspacing=0 border=0>
<form METHOD=”post” NAME=”rating” ACTION=”[code=php]<?php echo $_SERVER[‘PHP_SELF’]; ?>[/code]”>
<tr>
<td class=”ratebutton” background=”images/rate/1.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”1″> </td>
<td class=”ratebutton” background=”images/rate/2.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”2″></td>
<td class=”ratebutton” background=”images/rate/3.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”3″></td>
<td class=”ratebutton” background=”images/rate/4.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”4″></td>
<td class=”ratebutton” background=”images/rate/5.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”5″></td>
<td class=”ratebutton” background=”images/rate/6.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”6″></td>
<td class=”ratebutton” background=”images/rate/7.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”7″></td>
<td class=”ratebutton” background=”images/rate/8.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”8″></td>
<td class=”ratebutton” background=”images/rate/9.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”9″></td>
<td class=”ratebutton” background=”images/rate/10.gif” valign=”middle”><input TYPE=RADIO NAME=”rating” VALUE=”10″ checked>
<input type=”hidden” name=”id_value” value=”<?php print $id_to_rate;?>”/></td>
<td align=center><input TYPE=image src=”images/rate/rate_me.jpg” VALUE=”Rate me!”></td>
</tr>
</form>
</table>
</td> <!– rate bar –>
</tr>
<tr>
<td align=”center”>
[code=php]
<?php
echo “<img src=’/site/uploaded_files/”.basename($list[‘file’]).”‘ alt=”>”; // call image
}
if (array_key_exists(‘id_value’,$_POST))
{
$id_to_rate = ereg_replace(“[^A-Za-z0-9]”, “”, $_POST[‘id_value’]);
print $id_to_rate;
$new_rating = $_REQUEST[‘rating’];
$query = “SELECT total, average_score, votes FROM accounts where id =’$id_to_rate'”; // select 1 random record from accounts
$results = mysql_query($query);
$scoreslist = mysql_fetch_array($results);
//Retrieve current values from database
$total = $scoreslist[‘total’];
$average_score = $scoreslist[‘average_score’];
$votes = $scoreslist[‘votes’];
//Calculate new values
$new_votes = $votes + 1;
$new_total = $total + $new_rating;
$new_average= $new_total / $new_votes;
$new_average= number_format($new_average, 2);
session_start();
$_SESSION[‘previousid’] = $id_to_rate;
//print “Average: “. $new_average;
//print “Votes: “. $votes . ” Total: “. $total .” Average: “. $average_score .”<br>”;
//print “Votes: “. $new_votes . ” Total: “. $new_total .” Average: “. $new_average;
$updatequery = “Update accounts set total = $new_total, average_score = $new_average, votes = $new_votes where id = $id_to_rate”;
if (!mysql_query($updatequery, $dbh))
{
die(‘Error: ‘.mysql_error());
}
echo “Storing…”;
mysql_close($dbh);
echo “<meta http-equiv=”Refresh” content=”0;url=”.$HTTP_SERVER_VARS[php_SELF].””>”;
}
?>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan=”2″ vAlign=top bgcolor=”#FFFFFF”><br> <br></td>
</tr>
</table>
<br>
<table width=760 border=0 cellspacing=0 cellpadding=0>
<tr>
<td height=1 bgcolor=B2B3B3></td>
</tr>
<tr align=center>
<td height=29><a href=”info.php” class=”linksFooter”>About Us</a><img src=”Images/footer_seperator.gif” width=”15″ height=”25″><a href=”contact.php” class=”linksFooter”>Contact
Us</a><img src=”Images/footer_seperator.gif” width=”15″ height=”25″><a href=”terms.php” class=”linksFooter”>Terms
and Conditions</a><img src=”Images/footer_seperator.gif” width=”15″ height=”25″><a href=”privacy.php” class=”linksFooter”>Privacy</a></td>
</tr>
<tr>
<td height=2 bgcolor=0B9BFE></td>
</tr>
<tr>
<td height=25 align=center>© 2006 sample</td>
</tr>
</table>
</div>
</body>
</html>