User Profile tutorial

Post here is you are having problems with any of the tutorials.
Post Reply
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

User Profile tutorial

Post by wignall6 »

Hello,

I am implementing the user profile system into your login system. On the edit profile page it isn't grabbing the users information. I have changed the session parts to the session in the login but it still doesn't get the information.

This is my edit_profile.php -
<?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('#^[1-z0-9 ]+$#i', $_POST['location']) === 0) {
		$errors[] = 'Your location must only contain a-z, 0-9 and 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['username']);
	}

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

?>
and this is my fetch_user_info function -
// Fetches profile information for the given user.
function fetch_user_info($uid) {
	$uid = (int)$uid;
	
	$sql = "SELECT
				`user_name` AS `username`,
				`first_name` AS `firstname`,
				`last_name` 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);
}
The fetch_user_info shouldn't matter anyway, but in the PHP of edit_profile.php it sets the session to 'username' and that's the session set in my login.php.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile tutorial

Post by jacek »

You are passing the username to a functions which expects a user ID.
Image
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

Re: User Profile tutorial

Post by wignall6 »

Yeah, how would I make it so the page will get the userid to display the right information.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile tutorial

Post by jacek »

Probably best to store the user id in the session too.
Image
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

Re: User Profile tutorial

Post by wignall6 »

How would I store two values in the session?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile tutorial

Post by jacek »

wignall6 wrote:How would I store two values in the session?
In the exact same was as you store one.
$_SESSION['thing_1'] = 'value_1';
$_SESSION['thing_2'] = 'value_2';
Image
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

Re: User Profile tutorial

Post by wignall6 »

Would this work for getting the user_id in your login system?
$_SESSION['user_id'] = htmlentities($_GET['user_id']);
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: User Profile tutorial

Post by jacek »

Depends how you are using it, probably not, since you will not likely have the id in the url.

There is no need for htmlentities either.
Image
Post Reply