Jump to content

MySQL error with User profile system


jaymeh

Recommended Posts

Hi All,

 

I am currently struggling with the my user info function which is supposed to display an image on my profile page along with the following parts of information taken from my database.

 

The error is a mysql error stating that the $info = mysql_fetch_assoc($result); is not a valid arguement.

 

function fetch_user_info($uid)
{

$uid=(int)$uid;
       
    $sql = "SELECT
			`user_id AS `id`
                `user_username` AS `username`,
                `user_firstname` AS `firstname`,
                `user_lastname` AS `lastname`,
                `user_email` AS `email`,
                `user_location` AS `location`,
                `user_about` AS `about`,
                `user_gender` AS `gender`
			FROM `users`
			WHERE `user_id` = {$uid}";
                                 
        $result = mysql_query($sql);
       
        $info = mysql_fetch_assoc($result);
       
        $info['avatar'] = "core/user_avatars/{$info['id']}.jpg";

	return $info;

}

 

I have looked through the code a few times, but I can tell what is wrong.

 

I have included below the profile page code in case it may be an issue there.

 

<?php

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

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

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $userinfo ['username']; ?>'s Profile</title>
</head>
<body>
<div>
    
    <?php

	if($userinfo == false)
	{

		echo 'Sorry, the user does not exist.';

	}
	else
	{	
	?>
        	<h1><?php echo $userinfo ['firstname']; ?> <?php echo $userinfo ['lastname']; ?></h1>
            <img src="<?php echo $userinfo['avatar'];?>" alt="avatar"/>
    		<p>Username: <?php echo $userinfo ['username']; ?></p>
   			<p>First Name: <?php echo $userinfo ['firstname']; ?></p>
    		<p>Last Name: <?php echo $userinfo ['lastname']; ?></p>
            <p>Gender: <?php echo ($userinfo ['gender'] == 1) ? 'Male' : 'Female'; ?></p>
    		<p>Email: <?php echo $userinfo ['email']; ?></p>
   			<p>Location: <?php echo $userinfo ['location']; ?></p>
    		<p>About: <?php echo $userinfo ['about']; ?></p>
            
    	</div>
    <?php
	}
?>
</body>
</html>

 

Thanks

 

Jamie

Link to comment
Share on other sites

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /web/stud/u0963643/userprofilesection/finaluserprofile/core/inc/user.inc.php on line 43

 

Notice: Undefined index: firstname in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 29

 

Notice: Undefined index: lastname in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 29

 

Username:

Notice: Undefined index: username in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 31

First Name:

Notice: Undefined index: firstname in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 32

Last Name:

Notice: Undefined index: lastname in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 33

Gender:

Notice: Undefined index: gender in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 34

Female

 

Email:

Notice: Undefined index: email in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 35

Location:

Notice: Undefined index: location in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 36

About:

Notice: Undefined index: about in /web/stud/u0963643/userprofilesection/finaluserprofile/profile.php on line 37

 

<?php

error_reporting(E_ALL);

//fetches all of the users from the table

function fetch_users()
{
$result = mysql_query('SELECT user_id AS `id`, user_username AS `username` FROM users');

$users = array();

while($row = mysql_fetch_assoc($result))
{

	$users[] = $row;

}

return $users;

}
//fetches profile information for the given user
function fetch_user_info($uid)
{

$uid=(int)$uid;
       
    $sql = "SELECT
			`user_id AS `id`
                `user_username` AS `username`,
                `user_firstname` AS `firstname`,
                `user_lastname` AS `lastname`,
                `user_email` AS `email`,
                `user_location` AS `location`,
                `user_about` AS `about`,
                `user_gender` AS `gender`
			FROM `users`
			WHERE `user_id` = {$uid}";
                                 
        $result = mysql_query($sql);
       
        $info = mysql_fetch_assoc($result);
       
        $info['avatar'] = "core/user_avatars/{$info['id']}.jpg";

	return $info;

}

function fetch_user_id($username){
        
	$username = mysql_real_escape_string($username);

        $result = mysql_query("SELECT`user_id` FROM `users` WHERE `user_username` = '{$_SESSION['username']}'");       

        return mysql_result($result,0);
}

//updates the current users profile info
function set_profile_info($email, $location, $about, $avatar)
{
       $email          = mysql_escape_string(htmlentities($email));
       $about          = mysql_escape_string(nl2br(htmlentities($about)));
       $location       = mysql_escape_string($location);
       
   if (file_exists($avatar))
   {
		$src_size = getimagesize($avatar);

		if($src_size['mime'] === 'image/jpeg' || $src_size['mime'] === 'image/png' || $src_size['mime'] === 'image/gif')
		{
			$target_path = "core/user_avatars/";
                $target_path .= basename($_SESSION['uid'].'.jpg');
               
                if(move_uploaded_file($_FILES['avatar']['tmp_name'],$target_path)){
                                echo "The file has been uploaded";

                } else {
                        echo "There was an error";     
                }	
		}
		else
		{
			$src_img = false;	
		}
		if ($src_img != false)
		{
			$thumb_width = 200;

			if ($src_size[0] <= $thumb_width)
			{
				$thumb = $src_img;	
			}
			else
			{
				$new_size[0] = $thumb_width;
				$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;

				$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
				imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
			}

			imagejpeg($thumb, "core/user_avatars/{$_SESSION['uid']}.jpg");
		}


   }
       
       $sql = "UPDATE `users` SET
                                `user_email` = '{$email}',
                                `user_about` = '{$about}',
                                `user_location` = '{$location}'
                               
                        WHERE `user_id` = {$_SESSION['uid']}";
       
       mysql_query($sql);
       
}

//checks if the given username exists in the database
function user_exists($user)
{
$user = mysql_real_escape_string($user);

$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}'");

return (mysql_result($total, 0) == '1') ? true : false;
}

//checks if the given username and password combination is valid
function valid_credentials($user, $pass) {
$user = mysql_real_escape_string($user);
$pass1 = sha1($pass);
$query = "SELECT user_id FROM users WHERE user_username = '$user' AND user_password =  '$pass1'";
$result = mysql_query($query);
if(mysql_num_rows($result)>0) {
	$row = mysql_fetch_array($result);
	return $row['user_id'];
}else{
	return 0;
}
}

//adds a user to the datatabase
function add_user($user, $pass)
{
$user = mysql_real_escape_string(htmlentities($user));
$pass = sha1($pass);
$joined = date("d-m-Y");

mysql_query("INSERT INTO `users` (`user_username`, `user_password`, `user_joined`) VALUES ('{$user}', '{$pass}', '{$joined}')");
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.