/    Sign up×
Community /Pin to ProfileBookmark

Can anyone see why this is happening…PLEASE

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[/COLOR]

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());
?>[/code]

<!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>–>

[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’];

?>
[/code]

<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=”[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].””>”;
}
?>[/code]

</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>

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Mar 12.2007 — I see nothing in your code that's would be causing the 0.1 sec flashback.

It's "probably" a browser issue. Does this happen with all browsers?

One thing you could try to use would be [b]ob_start()[/b] and [b]ob_end_flush()[/b], though I'm not sure it would help much as loading the image is not included in the output buffering.

You can also look around for so-called javascript "image preloaders", to make sure the image is "in" the browser as it begins displaying the page.
Copy linkTweet thisAlerts:
@oo7mlauthorMar 13.2007 — it happens in both browsers, no one can seem to find out what is causing it....eeerrh...
Copy linkTweet thisAlerts:
@aj_nscMar 13.2007 — EDIT: I had the wrong idea in this post, please see my next one

Ok, lets think about this, whats happening the first time is that everything runs smoothly, then something must get changed which makes it different the second time it runs. And then what's happening is the last block of php code you have there, is getting executed in its entirety when you dont want that. You want to either echo the image, OR refresh the page (am I right?, if not, please clarify).

So that means that when you go to the page first, there is no $_POST['id_value'] because you just went to the url, nothing got posted, then when you rate a photo, this value gets set and the page refreshes and shows another photo normally.

I can't put my finger on it, (because I don't want to spend all day on this thread or anything) but I think it has something to do with the refresh with $HTTP_SERVER_VARS[php_self]

If I were you, I would start echoing out every variable that is used to do something at the beginning of your document so you can see what they are all set to and see how they change through the course of executing the script. I think we can figure this out
Copy linkTweet thisAlerts:
@aj_nscMar 13.2007 — Wait a second, I think I understood the question wrong, the page generates the photo twice over EVERY TIME THIS SCRIPT IS EXECUTED?? I thought you meant it only generated the photo twice over the second time it was executed. I can understand completely why it does this, you're using some bad logic here in my opinion, you're using a refresh to make a query if somebody has just voted for a picture AFTER the page loads meaning that a new picture will load, then the refresh will take place, and then another new picture will load.

So, I think you need to rearrange this script.

I think it would be best to evaluate if $_POST['id_value'] exists in the header of the document (get all the queries from the previous photo being rated out of the way first, then deal with the matter at hand). And there's no need for having a refresh at all. After you've done everything concerning the last record, you just start anew and grab another one.

Let me know what you think.
Copy linkTweet thisAlerts:
@oo7mlauthorMar 13.2007 — Thanks a million for taking time out to look at this, i will try your theory as soon as i get back, thanks again, i really appreciate it
Copy linkTweet thisAlerts:
@aj_nscMar 13.2007 — I'm interested to know how it works out, let me know.
×

Success!

Help @oo7ml spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...