page isn't updating when refreshed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
geoffmuskett
Forum Newbie
Posts: 22
Joined: Sat Feb 27, 2010 3:09 pm

page isn't updating when refreshed

Post by geoffmuskett »

Hi, I have a form which allows users to select a profile picture which is an animal. It uses <?php echo $_SERVER['PHP_SELF'] ?> because I want it to update the picture when the user hits submit, but for some reason it will only display the chosen picture if the user hits submit twice. However the database does update with the first hit of submit.

Here's the form...

Code: Select all

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">

	<br />Choose your profile picture or start looking after your pet horse<br />
    
    Horse <input type="radio" name="1horse" value="1"/><br />
    
    Cat <input type="radio" name="2cat" value="2"/><br />
    
    Dog <input type="radio" name="3dog" value="3"/><br />
    
    <input type="submit" name="pet_submit" />
 </form>
This checks which picture number is stored in the database and displays the relevant image and the form if the picture number is 2 or 3 ( I realise this should probably be a switch statement):

Code: Select all

// Collects data from "horse" table
	$horse_profile_pic_query2 = mysql_query("SELECT * FROM users WHERE id='$member_id'")
	or die(mysql_error());
	
	// puts the "users" info into the an array
	$horse_profile_pic_result2 = mysql_fetch_array( $horse_profile_pic_query2 ); 	
	
	
	if ( $horse_profile_pic_result2['profile_pic_id'] == 1) {
		
		echo "<br /><br /><img src=\"a/images/1horse.jpg\" />";
		
	} elseif ( $horse_profile_pic_result2['profile_pic_id'] == 2) {
		
		echo "<br /><br /><img src=\"a/images/2cat.jpg\" /><br />";
		include	"a/inc/profile-pic-form.php";
		
	} elseif ( $horse_profile_pic_result2['profile_pic_id'] == 3) {
		
		echo "<br /><br /><img src=\"a/images/3dog.jpg\" />";
		include	"a/inc/profile-pic-form.php";
		
	} else {
		echo "<br /><br /><img src=\"a/images/7default.jpg\" /><br />";
		include	"a/inc/profile-pic-form.php";
	}
and this updates the database with the selected image:

Code: Select all

	// if value is submitted insert into database
	if (isset($_POST['1horse'])) {
		//set $image to chosen value
		$image =  $_POST['1horse'];
		//update $users table with chosen value
		mysql_query("UPDATE users SET profile_pic_id = $image WHERE username = '$member_username'");
	} elseif (isset($_POST['2cat'])) {
		//set $image to chosen value
		$image =  $_POST['2cat'];	
		//update $users table with chosen value
		mysql_query("UPDATE users SET profile_pic_id = $image WHERE username = '$member_username'");
	} elseif (isset($_POST['3dog'])) {
		//set $image to chosen value
		$image =  $_POST['3dog'];	
		//update $image table with chosen value
		mysql_query("UPDATE users SET profile_pic_id = $image WHERE username = '$member_username'");
	} 
Not sure why it's not updating on first hit of submit...any ideas?

Thanks
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: page isn't updating when refreshed

Post by mecha_godzilla »

Because this script is calling itself, is it possible that the first time it's trying to display the image before the database record has been updated, then it works as expected the second time because the value is now in the database? I wonder if some kind of 'race condition' is happening in your script...

Can you post the full script please rather than just sections of it, then it might be possible to see what's going on better. From a development perspective, I usually separate my view/edit pages into different scripts (probably not as optimal as having everything in one script, but it's easier to debug!) so you might want to also consider this approach if the problem can't be resolved quickly.

HTH,

Mecha Godzilla
geoffmuskett
Forum Newbie
Posts: 22
Joined: Sat Feb 27, 2010 3:09 pm

Re: page isn't updating when refreshed

Post by geoffmuskett »

Thanks for the reply Mecha Godzilla,

I separated the form and the result page and it now works lovely. I'm sure you're right about displaying the image before the database record has been updated. The separating solution suits me fine though.

Cheers,

Geoff
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: page isn't updating when refreshed

Post by mecha_godzilla »

As the great Larry Wall once said..."Whatever works!"

Good luck with the rest of your project anyway - if you need any more input just say so.

M_G
Post Reply