PHP Tutorial: User Profiles problem

Post here is you are having problems with any of the tutorials.
Post Reply
MariosSal
Posts: 2
Joined: Sat Apr 07, 2012 11:21 pm

PHP Tutorial: User Profiles problem

Post by MariosSal »

I have followed the tutorial on how to create users profiles. I followed all code line by line but i have a problem with the uid variable.

I am testing it on windows 7 using wamp server.

Also to make the sql statements working i had to remove most of the quotes. I don't know why the code from the tutorial is not working for me.

Below is the code of my files please check them.

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('#^[a-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['uid']);
}

$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">
<html 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 { float:left; width:100px; }
			input[type="text"], textarea { float:left; width:400px; }
			input[type="submit"] { margin:10px 0px 0px 100px; }
		</style>
		<title>Edit Your Profile</title>
	</head>

	<body>
		<div>
			<?php
			
			if (isset($errors) === false){
				echo 'Click update to edit your 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">Lcation:</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 strip_tags($user_info['about']); ?></textarea>
			</div>
			<div>
				<input type="submit" value="Update" />
			</div>
		</form>
	</body>
</html>
profile.php
<?php

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

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


?>

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

	<body>
		<div>
			<?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 ? 'Male' : 'Female'; ?></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>
	</body>
</html>
user_list.php
<?php

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

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Registered Users</title>
	</head>

	<body>
		<div>
			<?php
			
			foreach(fetch_users() as $user){
				?>
				<p>
					<a href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a>
				</p>
				<?php
			}
			
			?>
		</div>
	</body>
</html>
init.inc.php
<?php

session_start();

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

$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");

$_SESSION['uid'] = 1;

?>
user.inc.php
<?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);
	
	return mysql_fetch_assoc($result);
}

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

?>
Thank you in advance!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: User Profiles problem

Post by jacek »

It was probably removing the quotes that broke it,

Some of the names used in the querys are actually mysql keywords and the quotes you removed should have been backticks, those tell MySQL that the thing between them is the name of a column or table.

So as an example it should be
SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users`
Try adding backticks around anything that is the name of a column or table.
Image
MariosSal
Posts: 2
Joined: Sat Apr 07, 2012 11:21 pm

Re: PHP Tutorial: User Profiles problem

Post by MariosSal »

Thank you so much it solved my problem the backtips ` was the error !!!
Post Reply