I am trying to build a simple online quiz. It requires no user input. All it does is randomly select an entry from a mysql database list of names, finds and image for that name and then displays the image. The problem i'm having is I would like to create a 'next' button which would refresh the mysql query and post a new image/question. This is what I currently have and while it does refresh the page, it does not get a new result. What am I missing here? Cache? cookies? Any thoughts are much appreciated.
<?php
$quiz_select = "SELECT * FROM wp_quiz ORDER BY RAND() LIMIT 1; ";
$result_select = mysql_query($quiz_select) or die(mysql_error());
while($row = mysql_fetch_array($result_select)) {
$name = stripslashes($row['comname']);
$sciname = stripslashes($row['sciname']);
$code = stripslashes($row['code']);
$image = stripslashes($row['image']);
}
$image = preg_replace('/\s+/', '', $image);
echo "<div style='margin-left: auto;margin-right: auto; width: 500px'><img style='max-width:500px;' src='http://$image.jpg.to'></div>";
?>
<form>
<input type="button" onClick="history.go(0)" value="Next">
</form>
By reloading from history, you're probably reloading a browser cached version of page, which will always be the same. You just need to load the page again.
(ie, change your form button to be a standard html button, and not to fire javascript history).
<form action="targetpage.php" method="post">
<input type="submit" value="Next">
</form>
PS, order by RAND() is not a great way to make your selection: http://www.webtrenches.com/post.cfm/avoid-rand-in-mysql
Maybe you should close the connection and free the result.
mysql_free_result($this->rs); mysql_close($this->conn);
I ended up having to unset a cookie on refresh.
<form action='link.com/page.php?next' method='post'>
<input type='submit' value='Next'>
</form>
</div>";
if(isset($_GET['next'])) {
setcookie ("PHPSESSID", "", time() - 3600);
}