...

View Full Version : reload page if query successful



murphyz
01-07-2004, 10:36 PM
Quite hard to explain, so shall try and if I make no sense, please ask.

Page is displayed taking info from a database depending on a particular field (user_riddle) of the user. Page also has a form at the end.

User enters details to the form and submits - which reloads the page.

If the data is false. the page reloads as before - no problem here.
If the data is true, user_riddle increments by 1, which should result in a different set of info coming out of the database.

However, the script runs as before, and I need to manually refresh the page for the database to be queried again to get the new info. I would like to reload the page after the user_riddle has incremented...how can I do this?


<?php
// Request the user_riddle number
global $userdata;
echo '<p>Riddle answer # ' .$userdata[user_riddle].'</p>';

// Request the text of selected riddle
global $userdata;
$currentanswer = @mysql_query("SELECT answer FROM phpbb_riddles WHERE riddleid = $userdata[user_riddle]");

if (!$currentanswer) {
die("<p>Error performing query: " . mysql_error() . "</p>");
}
// Display the text of selected riddle in a paragraph

while ( $row = mysql_fetch_array($currentanswer) ) {
$user_answer = $row['answer'];
echo '<p>Riddle answer '.$user_answer.'</p>';
}

// Check answer and update user riddle by one increment if correct
$youranswer = $_POST['youranswer'];
if ( $youranswer == $user_answer )
{
global $userdata;
echo( 'Well done ' .$userdata[username].', that was the right answer' );
$incrementin = $userdata[user_riddle] + 1;
global $userdata;
$sql = 'UPDATE `phpbb_users` SET `user_riddle` = user_riddle + 1 WHERE `username` = \'anonymous\' ';
if ( @mysql_query($sql) ) {
echo('<p>Update affected ' . mysql_affected_rows() .
' rows.</p>');
else {
die('<p>Error performing this effing update: ' . mysql_error() .
'</p>');
}
echo '<p>number is '.$incrementin.', okay?</p>';
} else {
echo( 'bad luck' );
}

// Request the text of selected riddle
global $userdata;
$currentriddle = @mysql_query("SELECT riddle FROM phpbb_riddles WHERE riddleid = $userdata[user_riddle] ");

if (!$currentriddle) {
die("<p>Error performing query: " . mysql_error() . "</p>");
}
// Display the text of selected riddle in a paragraph

while ( $row = mysql_fetch_array($currentriddle) ) {
$user_riddle = $row['riddle'];
echo '<p>Riddle '.$user_riddle.'</p>';
}

?>


The // Request the text of selected riddle part of the page is the part that is not querying the db on the UPDATE information that has just been entered - so I assume a reload must be used here.

Cheers

Mxx

Nightfire
01-07-2004, 11:18 PM
I'll have a look tomorrow for you if no one else answers before. Too tired to do anymore coding today or even look at any code lol

murphyz
01-07-2004, 11:22 PM
A lifesaver once again...thanks.

Luckily there's only a few more things that need doing to the script before it's complete. :)

Mxx

raf
01-08-2004, 12:21 AM
Shouldn't

WHERE riddleid = $userdata[user_riddle]

be

WHERE riddleid = $incrementin

?
Where on top of the page, you set
$incrementin =$userdata[user_riddle] ;
and then , if the answer was correct, you increment it like
$incrementin ++;

This way, the next ridle will be selected if the answer was true, but the same will be selected it the answer was false.

murphyz
01-08-2004, 09:50 PM
EDITED AT BOTTOM

Sorry raf, I'm not 100% sure what you mean by that...would it be possible for you to change the appropriate parts of the code to reflect what you suggested? I attempted to change them but the script wouldn't load properly.:(

Many thanks

EDIT: Scrub that. I redid it again and it works :) Cheers raf...that saves having to reload the page. woot!

Mxx

raf
01-08-2004, 10:31 PM
Glad you got it running :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum