Adding Avatars breaks error check

Post here is you are having problems with any of the tutorials.
Post Reply
SkillBuzz
Posts: 13
Joined: Wed Apr 04, 2012 7:50 am

Adding Avatars breaks error check

Post by SkillBuzz »

Hello,

I tried searching for this, but could not find it, so here it goes:

After following your tutorial for Avatar uploading, everything works like it should. Except when it doesnt..
Basically adding the avatar to the fetch_user_info seems to have broken the current error check:
if ($user_info === false) {
		echo 'That user does not exist';
since it no longer returns false (the avatar image always shows, regardless of)

so, either with mysite/profile.php or mysite/profile.php?uid=non_existing_id the error checking breaks.

I'll note that even without the avatar section, mysite/profile.php would always warn of an undefined 'uid'. But that i could live with, by turning errors off.


So, any help is appreciated. =)
SkillBuzz
Posts: 13
Joined: Wed Apr 04, 2012 7:50 am

Re: Adding Avatars breaks error check

Post by SkillBuzz »

actually, i fixed it with:
<?php
	if (isset($_GET['uid']) === false ) {
		echo 'No user selected';
	}elseif (array_key_exists('firstname', $user_info) === false) {
		echo 'No user';
the only thing im still seeing is the

"Notice: Undefined index: uid in profile.php on line 5"

when profile.php is called without ?uid=xxx..

but i think i can live with that
=)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Adding Avatars breaks error check

Post by jacek »

Didn't I make the function still return false if the user didn't exist ? That would be the best solution although your way is fine really :)

Also can you post the full code since line 5 has not mention of uid on it ;)
Image
SkillBuzz
Posts: 13
Joined: Wed Apr 04, 2012 7:50 am

Re: Adding Avatars breaks error check

Post by SkillBuzz »

Ah, see i'm still in the learning phase. :lol:

profile.php
<?php

include('includes/init.inc.php');

$user_info = fetch_user_info($_GET['uid']);

?>
<title><?php echo $user_info['username']; ?> 's Profile</title>
<div id="profilepage">
<?php
	if (isset($_GET['uid']) === false ) {
		echo 'No user selected';
	}elseif (array_key_exists('firstname', $user_info) === false) {
		echo 'No user';
	
	}else{
		?>
	<h1><?php echo ($user_info['firstname']), ' ', ($user_info['lastname']); ?></h1>
	<img src="<?php echo $user_info['avatar']; ?>" alt="Profile Image" />
    <p>Username:<?php echo $user_info['username']; ?></p>
  	<p>Email:<?php echo $user_info['email']; ?></p>
   	<p>Location:<?php echo $user_info['location']; ?></p>
    <p><?php echo $user_info['about']; ?></p>
    <?php 
	}
	?>
</div>

the function:
//Fetch profile info for current user.
function fetch_user_info($uid){
	$uid = (int)$uid;
	
	$sql = "SELECT
				`user_id` AS `id`,
				`username`, 
				`firstname`,
				`lastname`,
				`email`,
				`about`,
				`location`
			FROM `buzzers`
			WHERE `user_id` = {$uid}";
			
	$result = mysql_query($sql);
	
	$info = mysql_fetch_assoc($result);
    $info['avatar'] = (file_exists("{$GLOBALS['path']}/user_avatars/{$info['id']}.jpg")) ?  "includes/user_avatars/{$info['id']}.jpg" : "includes/user_avatars/default.jpg";
	return $info;
}
Out of curiousity, how would you have changed the function?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Adding Avatars breaks error check

Post by jacek »

Okay so line 5 is
$user_info = fetch_user_info($_GET['uid']);
that makes a lot more sense.

The error happens because you are trying to use a variable that does not exists, you can use isset to make sure that it does exist
if (isset($_GET['uid'])){
    $user_info = fetch_user_info($_GET['uid']);
}
would stop it happening.

How I would modify the function would be to add a check to see if the user info was returned, if it was not just return false, something like this
    //Fetch profile info for current user.
    function fetch_user_info($uid){
            $uid = (int)$uid;
           
            $sql = "SELECT
                                    `user_id` AS `id`,
                                    `username`,
                                    `firstname`,
                                    `lastname`,
                                    `email`,
                                    `about`,
                                    `location`
                            FROM `buzzers`
                            WHERE `user_id` = {$uid}";
                           
            $result = mysql_query($sql);
           
            $info = mysql_fetch_assoc($result);
            
            if ($info === false){
                    return false;
            }
            
            $info['avatar'] = (file_exists("{$GLOBALS['path']}/user_avatars/{$info['id']}.jpg")) ?  "includes/user_avatars/{$info['id']}.jpg" : "includes/user_avatars/default.jpg";
            
            return $info;
    }
If you use this method you could simplify the error checking a bit by using empty()
        if (empty($user_info)) {
                echo 'Invalid user';
        }else{
                // show the info.
        }
because empty() does not emit the error if the variable does not exist.
Image
Post Reply