User Profile tut not working

Post here is you are having problems with any of the tutorials.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile tut not working

Post by Temor »

Are you giving $_GET['id'] a value in the url?
Example:
localhost/profile.php?id=2

the problem seems to be that the $uid variable in fetch_user_info is empty, which in turn means $_GET['id'] is empty.
roclimb
Posts: 17
Joined: Tue Feb 07, 2012 9:12 pm

Re: User Profile tut not working

Post by roclimb »

I tried what you just said...entering an id in the browser and that doesn't work either. I tried with 0, 1, and 2. Same message....user does not exist. Am I supposed to specify the user better someplace in the PHP code????
Why does this code not work for me?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile tut not working

Post by Temor »

The only way to get the " User does not exist " message is if mysql_query returns false in fetch_user_info. For it to return false it must fail to get the requested information, and since we established that there's nothing wrong with the query itself it must be that you're not providing it with a valid ID. Are you absolutely sure you're giving it an ID that matches a user in your database?

Try passing in an ID manually, like this:
$user_info = fetch_user_info(1);
Make sure the ID matches a user in your table and if it still gives you the same error I would suggest re-writing it. It may be something small that we all fail to spot.
roclimb
Posts: 17
Joined: Tue Feb 07, 2012 9:12 pm

Re: User Profile tut not working

Post by roclimb »

Finally!!!!!!

That worked.

I removed this: ($_GET['uid']); Still can't understand what was wrong there?

Thanks so much for taking the time to help. I actually think I learned a great deal about php through this.....I still have a very, very, long way to go though, obviously.

thanks again for all your time. It was very kind of you to invest as much time as you did walking me through all this.
Cheers
Rob
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile tut not working

Post by Temor »

It could be partially my bad. I'm not really an expert at explaining things.

$_GET['uid'] will only be set if you have ?uid= something in the url. Whatever goes in the brackets ( [' '] ) must be the same as in the url. So $_GET['name'] would require a url like this: www.website.com/profile.php?name=Name

I do my best in helping out, but it seems that sometimes I tend to explain things in a hard-to-understand manner which can cause quite some confusion. I'm sorry if anything I said put you off track and I hope to see you here again soon!
The more errors you encounter, the more you'll learn!
roclimb
Posts: 17
Joined: Tue Feb 07, 2012 9:12 pm

Re: User Profile tut not working

Post by roclimb »

Ok, I see. But I'm on a localhost MAMP. How can I make this program change the URL? Is that possible. All my url says is the name of the file profile.php.

I also just realized I can't continue on with this tutorial because I have to connect to the session for the last part with the edit_profile page and I won't be able to do that.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile tut not working

Post by Temor »

roclimb wrote:Ok, I see. But I'm on a localhost MAMP. How can I make this program change the URL? Is that possible. All my url says is the name of the file profile.php.

I also just realized I can't continue on with this tutorial because I have to connect to the session for the last part with the edit_profile page and I won't be able to do that.
Changing the URL? All you have to do to change the URL is click in the address bar and type what you want.
If you want to create a link to a users profile you would just have to create a link.
<a href="profile.php?uid=X">User X's profile</a>
Change X to a users ID.

And why won't you be able to connect to the session? MAMP is not, as far as I know, blocking sessions in any way. You should be able to work with sessions just as you would on any other system with any other Apache, MySQL & php package.
roclimb
Posts: 17
Joined: Tue Feb 07, 2012 9:12 pm

Re: User Profile tut not working

Post by roclimb »

yeah, well I died that before and it does not work. I manually added the user id to the url and got nothing. unless I add the value to the user manually like you showed me the tut does not find the kid and does not work, hence, i can't complete the last part of the tut
roclimb
Posts: 17
Joined: Tue Feb 07, 2012 9:12 pm

Re: User Profile tut not working

Post by roclimb »

This is my current code and my page is invisible again and not working.
<?php

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)) !== false){
        $users[] = $row;
    }

	return $users;
}

function fetch_user_info($uid){
	$uid = (int)$uid;


	$sql = "SELECT 
				`user_username` 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);			
}

//updates current users profile info
function set_profile_info($email, $about, $location){
	$email =       mysql_real_escape_string(htmlentities($email));
	$about =       mysql_real_escape_string(htmlentities(nl2br($about));
	$location =    mysql_real_escape_string(htmlentities($location));

    $sql = "UPDATE `users`  SET
             `user_email` = '{$email}',
             `user_about` = '{$about}',            
             `user_location` = '{$location}'
          WHERE `user_id` = '{$uid}'";

          
      mysql_query($sql);      
}


?>
and
<?php

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

if (isset($_POST['email'],$_POST['location'],$_POST['about'])){
	$errors = array();
	
	if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
	 $errors[] = 'The email address you entered is not valid.';
	}
	
	if (preg_match('#^[a-z0-9 ]+$#i', $_POST['location']) === 0){
	   $errors[] = 'Your location must only contain a-z, 0-9 spaces.';
	}
	
	if (empty($errors)){
	  set_profile_info($_POST['email'], $_POST['about'], $_POST['location']);
	  
	}
	
	$user_info = array(
	'email'  => htmlentities($_POST['email']),
	'about'  => htmlentities($_POST['about']),
	'location'  => htmlentities($_POST['location'])
  );
	
}else{

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


}


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<htm xmlns="http://www.w3.org/1999/xhtml">



	<head>
	
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	
	<style type="text/css">
		form { margin:10px 0px 0px 0px;}
		form div {float: left; clear: both; margin:0px 0px 4px 0px; }
		label[type="text"], textarea {float:left; width:400px; }
		input[type="submit"], {margin:10px 0px 0px 100px;}
	</style>
		
		
		<title></title>
	</head>
	<body>
		<div>
		<?php
	
	if (islet($errors )=== false){
	   echo 'Click to update profile.';
	}else if (empty($errors)){
	   echo 'Your profile has been updated/';
	}else{
	   echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
	}
	
	
	?>
	
	</div>
	<form action="" method="post">
		<div>
			<label for="email">Email:</label>
			<input type="text" name="email" id="email" value="<?php echo $user_info['email']; ?>" />
		</div>
		<div>
		    <label for="location">Location:</label>
			<input type="text" name="location" id="location" value="<?php echo $user_info['location']; ?>" />
        </div>
        <div>
        	<label for="about">About Me:</label>
			<textarea name="about" id="about" rows="14" cols="50"><?php echo $user_info['about']; ?></textarea>
		</div>
		<div>
			<input type="submit" value="update" />
		</div>
	</form>
	
	</body>
</html>

I have tried everything I can think of to pass the user id and nothing works.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile tut not working

Post by Temor »

you forgot a $ before user_info.

also, if you're setting it equal to $_SESSION['id'] you will only be able to see the profile of the currently logged in user.
Post Reply