User Profile System - Info for edit_profile not showing :(i

Post here is you are having problems with any of the tutorials.
Post Reply
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

User Profile System - Info for edit_profile not showing :(i

Post by soshannad »

Sorry to give you two posts in 1 day, but I am banging my head against the wall trying to figure out what I've missed, and after re-watching the videos numerous times, I just can't find it.

I'm trying to do the Member Profile tutorials and I have everything working thus far, the user list works great, the profile pages work great, but when I get to the edit profile page, it only displays the form, but there's no data in it.

I've dropped the page to bare minimum and tried to just echo out a user name to see if it would at least display that, and it doesn't. I check the protected page to make sure I'm still logged in as a user and I am. So, I'm not sure why it won't recognize the user session on the edit profile page. Anyhow, here's my code - sorry that it's mixed in with my other page code, but that shouldn't be affecting it as it's the same code on all the other pages that work just fine.

Here it is -

init.inc.php
<?php

session_start();

$exceptions = array('register', 'login');

$page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4);

mysql_connect('localhost', 'root', 'root');
mysql_select_db('user_system');

$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");
//added variable in profile tutorial
$_SESSION['uid'] = 1;
//end added variable
if (isset($_COOKIE['username'], $_COOKIE['password']) && isset($_SESSION['username']) === false){
	
	if (valid_credentials($_COOKIE['username'], $_COOKIE['password'])){
		
		$_SESSION['username'] = htmlentities($_COOKIE['username']);
		
		setcookie('username', $_COOKIE['username'], time() + 604800);
		setcookie('password', $_COOKIE['password'], time() + 604800);
	}
}

if (in_array($page, $exceptions) === false){
	
	if (isset($_SESSION['username']) === false){
		
		header('Location: login.php');
		die();
	}
}



?>
user.inc.php
<?php

//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_name` = '{$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);
	$pass = mysql_real_escape_string($pass);
	$total = mysql_query("SElECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$pass}'");	
	return (mysql_result($total, 0) == '1') ? true : false;
}

//adds a user to the database
function add_user($user, $pass){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);	
	mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}', '{$pass}')");
}

//fetches all of the users from the table.
function fetch_users(){
	$result = mysql_query('SELECT `user_id` AS `id`, `user_name` AS `username` FROM `users` ');
	$users = array();
	while (($row = mysql_fetch_assoc($result)) !== false){	
		$users[] = $row;
	}	
	return $users;
}

//fetches profile information for the given user
function fetch_user_info($uid){
	$uid = (int)$uid;
	
	$sql = "SELECT
					`user_name` AS `username`,
					`user_firstname` AS `firstname`,
					`user_lastname` AS 'lastname',
					`user_email` AS `email`,
					`user_about` AS `about`,
					`user_location` AS `location`,
					`user_gender` AS `gender`
				FROM `users`
				WHERE `user_id` = {$uid}";
	$result = mysql_query($sql);
	return mysql_fetch_assoc($result);
}
?>
user_list.php
<?php 

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

?>

<!DOCTYPE html>
<html lang="en-US">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>informapet - Complete Resource for Pet Adoption</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <link rel="stylesheet" href="css/cupertino/jquery-ui-1.8.17.custom.css" type="text/css" />    
 		<script type="text/javascript" src="js/main.js"></script>
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script>
                
        <!--[if lt IE 9]>
        <script type="text/javascript">
        document.createElement("nav");
        document.createElement("header");
        document.createElement("footer");
        document.createElement("section");
        document.createElement("aside");
        document.createElement("article");
        </script>
        <![endif]-->

	  	<link rel="stylesheet" href="css/slide.css" type="text/css" media="screen" />

	  	<!-- PNG FIX for IE6 -->
	  	<!-- http://24ways.org/2007/supersleight-tra ... png-in-ie6 -->
		<!--[if lte IE 6]>
			<script type="text/javascript" src="js/pngfix/supersleight-min.js"></script>
		<![endif]-->


		<!-- Sliding effect -->
		<script src="js/slide.js" type="text/javascript"></script>
        
    </head>
    
    <body>
	


			<?php
	$path = $_SERVER['DOCUMENT_ROOT'];
	$path.= "/includes/logo.php";
	include_once ($path);
	?>
	
			<?php
	$path = $_SERVER['DOCUMENT_ROOT'];
	$path.= "/includes/loginout.php";
	include_once ($path);
	?>
	

	        <?php
	$path = $_SERVER['DOCUMENT_ROOT'];
	$path.= "/includes/header.php";
	include_once ($path);
	?>
        
        <section id="posts">
            <article class="post">
                <header>
                    <h2>User List</h2>
                </header>   

 				<section>
				<?php
				
				foreach (fetch_users() as $user){
					?>
					<p>
						<a href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a>
					</p>
					<?php
				}
				
				?>
				</section>
            </article>
        </section>
        
        <?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path.= "/includes/sidebar.php";
include_once ($path);
?> 
        <?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path.= "/includes/footer.php";
include_once ($path);
?>    
    </body>
    
</html>
profile.php
<?php 

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

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

?>

<!DOCTYPE html>
<html lang="en-US">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>informapet - <?php echo $user_info['username']; ?>'s profile</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <link rel="stylesheet" href="css/contact.css" type="text/css">
        <link rel="stylesheet" href="css/cupertino/jquery-ui-1.8.17.custom.css" type="text/css" />    
 		<script type="text/javascript" src="js/main.js"></script>
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script>
              
        <!--[if lt IE 9]>
        <script type="text/javascript">
        document.createElement("nav");
        document.createElement("header");
        document.createElement("footer");
        document.createElement("section");
        document.createElement("aside");
        document.createElement("article");
        </script>
        <![endif]-->
        
			<link rel="stylesheet" href="css/slide.css" type="text/css" media="screen" />

		  	<!-- PNG FIX for IE6 -->
		  	<!-- http://24ways.org/2007/supersleight-tra ... png-in-ie6 -->
			<!--[if lte IE 6]>
				<script type="text/javascript" src="js/pngfix/supersleight-min.js"></script>
			<![endif]-->


			<!-- Sliding effect -->
			<script src="js/slide.js" type="text/javascript"></script>

	    </head>

	    <body>



				<?php
		$path = $_SERVER['DOCUMENT_ROOT'];
		$path.= "/includes/logo.php";
		include_once ($path);
		?>

				<?php
		$path = $_SERVER['DOCUMENT_ROOT'];
		$path.= "/includes/loginout.php";
		include_once ($path);
		?>


	        <?php
	$path = $_SERVER['DOCUMENT_ROOT'];
	$path.= "/includes/header.php";
	include_once ($path);
	?>
        
        <section id="posts">
            <article class="post">
                <header>
                    <h2>Profiles</h2>
                </header>    

				<section>
					<?php
					
					if ($user_info === false){
						echo 'That user does not exist.';
					}else{
						?>
					<h1><?php echo $user_info['firstname']; ?> <?php echo $user_info['lastname']; ?></h1>
					<p>Username: <?php echo $user_info['username']; ?> </p>
					<p>Gender: <?php echo ($user_info['gender'] == 1) ? 'Female' : 'Male'; ?></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
						}
						?>
				
				</section>
            </article>

        </section>
        
        <?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path.= "/includes/sidebar.php";
include_once ($path);
?> 
        <?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path.= "/includes/footer.php";
include_once ($path);
?>    
    </body>
    
</html>
edit_profile.php
<?php 

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

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

?>

<!DOCTYPE html>
<html lang="en-US">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>informapet - <?php echo $user_info['username']; ?>'s profile</title>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <link rel="stylesheet" href="css/contact.css" type="text/css">
        <link rel="stylesheet" href="css/cupertino/jquery-ui-1.8.17.custom.css" type="text/css" />    
 		<script type="text/javascript" src="js/main.js"></script>
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script>
              
        <!--[if lt IE 9]>
        <script type="text/javascript">
        document.createElement("nav");
        document.createElement("header");
        document.createElement("footer");
        document.createElement("section");
        document.createElement("aside");
        document.createElement("article");
        </script>
        <![endif]-->
        
			<link rel="stylesheet" href="css/slide.css" type="text/css" media="screen" />

		  	<!-- PNG FIX for IE6 -->
		  	<!-- http://24ways.org/2007/supersleight-tra ... png-in-ie6 -->
			<!--[if lte IE 6]>
				<script type="text/javascript" src="js/pngfix/supersleight-min.js"></script>
			<![endif]-->


			<!-- Sliding effect -->
			<script src="js/slide.js" type="text/javascript"></script>

	    </head>

	    <body>



				<?php
		$path = $_SERVER['DOCUMENT_ROOT'];
		$path.= "/includes/logo.php";
		include_once ($path);
		?>

				<?php
		$path = $_SERVER['DOCUMENT_ROOT'];
		$path.= "/includes/loginout.php";
		include_once ($path);
		?>


	        <?php
	$path = $_SERVER['DOCUMENT_ROOT'];
	$path.= "/includes/header.php";
	include_once ($path);
	?>
        
        <section id="posts">
            <article class="post">
                <header>
                    <h2>Edit Profile</h2>
                </header> 

			
					<form action="" method="post">
						  <fieldset id="personal_information">
			                <legend>Edit Profile</legend>
			                <ol>
			                  <li>
			                    <label for="email">Email:</label>
			                    <input type="text" name="email" id="email" value="<?php echo $user_info['email']; ?>"/>
			                  </li>
			                  <li>
			                    <label for="email">Location:</label>
			                    <input type="text" name="location" id="location" value="<?php echo $user_info['location']; ?>"/>
			                  </li>
			                  <li>
			                    <label for="email">About Me:</label>
			                    <textarea name="about" rows="14" cols="50"><?php echo $user_info['about']; ?></textarea>
			                  </li>
			                  <li>
		
			                    <input type="submit" value="Update" />

			                  </li>
			                </ol>

			              </fieldset>
						</form>

            </article>
        </section>
        
        <?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path.= "/includes/sidebar.php";
include_once ($path);
?> 
        <?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path.= "/includes/footer.php";
include_once ($path);
?>    
    </body>
    
</html>
Again, I'm eternally grateful for all your help thus far and any help you can provide on this issue. Desperately trying to learn how to create a site from the ground up by myself and without your tutorials, I don't know where I'd be.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile System - Info for edit_profile not showing

Post by jacek »

If you add
var_dump($user_info);
under the fetch_user_info() call, what does it output ?
Image
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

It says bool(false)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile System - Info for edit_profile not showing

Post by jacek »

soshannad wrote:It says bool(false)
Okay, well I can't see anything right away that would cause that.

Try adding
echo mysql_error();
after the mysql_query() line in the function and see if that shows the problem.
Image
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

Well if you can't see it, it means I'm not crazy :)

Do you mean like this?
//fetches profile information for the given user
function fetch_user_info($uid){
	$uid = (int)$uid;
	
	$sql = "SELECT
					`user_name` AS `username`,
					`user_firstname` AS `firstname`,
					`user_lastname` AS 'lastname',
					`user_email` AS `email`,
					`user_about` AS `about`,
					`user_location` AS `location`,
					`user_gender` AS `gender`
				FROM `users`
				WHERE `user_id` = {$uid}";
	$result = mysql_query($sql);
	
	echo mysql_error();
    
	return mysql_fetch_assoc($result);
}
I did that and it only shows bool(false) again because of the var_dump. I tried removing the var_dump and then it showed nothing.
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

Is there some browser setting to show the mysql errors? I noticed that when you did your code wrong in the videos it returned the red text with the errors, whereas mine only showed a blank page.. maybe I have something turned off so that it won't show errors, but I'd assume your echo should override that.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile System - Info for edit_profile not showing

Post by jacek »

soshannad wrote:Is there some browser setting to show the mysql errors? I noticed that when you did your code wrong in the videos it returned the red text with the errors, whereas mine only showed a blank page.. maybe I have something turned off so that it won't show errors, but I'd assume your echo should override that.
Exactly, the echo mysql_error( will explicitly show any errors.

The only other thing it could be is that no rows are being returned then, which would suggest a problem with the session variable so try adding
var_dump($_SESSION['uid']);
just before the call to the function. If it does not look like your user id then there is the problem :)
Image
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

That returned int(1)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile System - Info for edit_profile not showing

Post by jacek »

soshannad wrote:That returned int(1)
Well that sounds right too...

What happens if you run the SQL manually in phpmyadmin ?
SELECT
   `user_name` AS `username`,
   `user_firstname` AS `firstname`,
   `user_lastname` AS 'lastname',
   `user_email` AS `email`,
   `user_about` AS `about`,
   `user_location` AS `location`,
   `user_gender` AS `gender`
FROM `users`
WHERE `user_id` = 1
Image
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

It says MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0004 sec )

As noted below: I don't have a user 1, so that's why it returned zero rows.
Last edited by soshannad on Wed Jan 18, 2012 1:50 am, edited 2 times in total.
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

I just changed the query to:
    SELECT
       `user_name` AS `username`,
       `user_firstname` AS `firstname`,
       `user_lastname` AS 'lastname',
       `user_email` AS `email`,
       `user_about` AS `about`,
       `user_location` AS `location`,
       `user_gender` AS `gender`
    FROM `users`
    WHERE `user_id` = 2
Changing the user_id to 2 from 1, and then it returned my information.

*just noticed that my "lastname" wasn't using backticks, so I fixed that, but it still isn't returning the results.
Last edited by jacek on Wed Jan 18, 2012 12:37 pm, edited 1 time in total.
Reason: code tags...
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

OK I added a user id=1 to my database.

and if I re-add this, which I had commented out since it was only in the member tutorial and seemed to only be in there assuming you had no database with multiple users.
//added variable in profile tutorial
$_SESSION['uid'] = 1;
//end added variable
into the init.inc.php the user id 1 works and the data pulls up, but not the user id 2. If I log in user id 2 it will show the edit profile info for user id 1.

So, maybe this is the magic factor. Since the tutorial for member had this and the login/register didn't - is there a way to alter it to have it be more effective for the member profile rather than just calling out user id = 1?

Here's a video of what's going on: http://youtu.be/DHL7m47WRrI
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile System - Info for edit_profile not showing

Post by jacek »

Well you obviously need the data in the database or there is nothing to show.
$_SESSION['uid'] = 1;
Was just to simulate someone being logged in, you can remove that completely if you have an actual login system.
Image
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

If I remove that completely, then we're back to square 1 where nothing works.

That's how I had it originally (commented out) when I first wrote this post.

Then, the edit_profile.php shows nothing for data.. and I have a section to call out the users page for a profile redirect and it just has http://localhost/profile.php?uid= in the browser search bar.

That code is:
<p><a href="profile.php?uid=<?php echo $_SESSION['uid']; ?>">Click here to view your profile!</a></p>
And it works if I have:

$_SESSION['uid'] = 1;

in there, but of course, only works for user id 1...
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

I do have data in my database.. I just didn't have a user id 1 - but now I do. I have a user id 1 and user id 2 and neither work.
soshannad
Posts: 13
Joined: Tue Jan 17, 2012 1:22 am

Re: User Profile System - Info for edit_profile not showing

Post by soshannad »

Thx for the help - I had a friend who knows PhP look at it and we had to fix some of the functions to accomodate the login/registration system with the member profile system as they don't work together by default. But it's all working now so I do appreciate you taking the time to look at this.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile System - Info for edit_profile not showing

Post by jacek »

soshannad wrote:Thx for the help - I had a friend who knows PhP look at it and we had to fix some of the functions to accomodate the login/registration system with the member profile system as they don't work together by default. But it's all working now so I do appreciate you taking the time to look at this.
No problem ;) Glad you got it working anyway.
Image
PhParker
Posts: 3
Joined: Sun Feb 05, 2012 6:57 pm

Re: User Profile System - Info for edit_profile not showing

Post by PhParker »

^ What did you do to fix the problem? I'm also having the same problem, and im trying to define $_SESSION['uid] to equal the current user id logged in, not a manually inputted one. Help?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile System - Info for edit_profile not showing

Post by Temor »

PhParker wrote:^ What did you do to fix the problem? I'm also having the same problem, and im trying to define $_SESSION['uid] to equal the current user id logged in, not a manually inputted one. Help?
We do appreciate if you'd create your own thread. Keeps things a bit neater.
Also, you'd want to set the $_SESSION['uid'] variable when your user logs in. There are several ways to do that. One way is running a query to fetch the ID corresponding to the username used to log in.
PhParker
Posts: 3
Joined: Sun Feb 05, 2012 6:57 pm

Re: User Profile System - Info for edit_profile not showing

Post by PhParker »

OK, I created a new thread. Sorry for the inconvenience.
Post Reply