Click to See Complete Forum and Search --> : random image twice over - why?


oo7ml
03-08-2007, 07:37 AM
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....

Here's my code below:





<?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'];

//print "ID=".$id_to_rate." <b>Current rating:</b> ". $current_score . " out of " . $current_votes . " voter(s)";
?>




<?php
echo "<img src='/site/uploaded_files/".basename($list['file'])."' alt=''>"; // call image

}

if (array_key_exists('id_value',$_POST))
{
//Get value from radio button
$id_to_rate = ereg_replace("[^A-Za-z0-9]", "", $_POST['id_value']);
print $id_to_rate;

$new_rating = $_REQUEST['rating'];

//Get values of results according to ID
$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]."\">";
}
?>

michael879
03-08-2007, 02:55 PM
taking a quick look at the code, I dont think you gave us enough. Can you post the entire file, including html? The problem might have the do with the refresh, but Im not sure. Are those two php blocks even in the same file?

oo7ml
03-09-2007, 06:46 AM
<?php
session_start();
//rate.php

// make connection to database
$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">&nbsp;</td>
</tr>
</table>-->

<?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%">&nbsp;</td>
<td width="15%">&nbsp;</td>
</tr>
<tr>
<td height="54" class="average">
<?php print $votedaverage;?>
</td>
<!-- average -->
<td>&nbsp;</td>
</tr>
</table></td>
</tr
><tr>
<td width="33%" height="62" class="rate_bottom_left">&nbsp;</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%">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</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="<?php echo $_SERVER['PHP_SELF']; ?>">
<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">

<?php
echo "<img src='/site/uploaded_files/".basename($list['file'])."' alt=''>"; // call image

}

if (array_key_exists('id_value',$_POST))
{
//Get value from radio button
$id_to_rate = ereg_replace("[^A-Za-z0-9]", "", $_POST['id_value']);
print $id_to_rate;

$new_rating = $_REQUEST['rating'];

//Get values of results according to ID
$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>&copy; 2006 sample</td>
</tr>
</table>

</div>

</body>

</html>

oo7ml
03-09-2007, 10:40 AM
Can anyone help with this.... PLEASE?