Page 1 of 1

User profile No Data Displayed

Posted: Thu Jun 09, 2011 4:28 pm
by kalipsso
Got it :)
Now i run into another issue...the edit_profile form.
is not displaying the values of the fields...
<?php

 include('core/init.inc.php');
 
 $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" xml:lang="en" lang="en">
<head>
	<!--<link rel="stylesheet" type="text/css" href="ext/css/style.css">-->
	<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
		
		
		
		?>	
	</div>
	<form action="" method="POST">
		<div>
			<label for="firstname">Firstname:</label>
			<input type="text" name="firstname" id="firstname" value="<?php echo $user_info['firstname']; ?>" />
		</div>
		<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:</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>
this is my edit_profile.php page.
what did i do wrong?
remember that i am using it with the register/login script and uses the same database called user_system...

Re: User profile No Data Displayed

Posted: Thu Jun 09, 2011 5:37 pm
by jacek
You can try adding
var_dump($user_info);
after that variable is defined to see what the function is actually returning.

Also can you make sure you use the php button when posting code, and not the code tags.

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 3:30 am
by kalipsso
jacek wrote:You can try adding
var_dump($user_info);
after that variable is defined to see what the function is actually returning.

Also can you make sure you use the php button when posting code, and not the code tags.
is not about displaying the data after submitting the form...is about that i get no user info in the form, the values from the database value="<?php echo $user_info['firstname']; ?>

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 8:02 am
by kalipsso
after i add
var_dump($user_info); 
the result returned NULL.

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 3:32 pm
by jacek
Okay, can you post the code of the function then ? It looks like that is where the problem will be.

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 6:00 pm
by kalipsso
does init.inc.php from user system conflicts wit the init.inc.php from the user profile ?

init.inc.php from user register/login:
<?php

session_start();

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

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

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

$path = dirname(__FILE__);

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

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', sha1($_COOKIE['password'], time() + 604800));
	}
}

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



?>
init.inc.php from user profile
<?php

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

$path = dirname(__FILE__);

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

$_SESSION['uid'] = 1;

?>
can these 2 be mixed together?
Thanks!
And user.inc.php from reg/login can be mixed with user.inc.php from user profile?

user.inc.php from login-reg
<?php

//checks if 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 given username and password conbination 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;
}

//checks if the given user account is active.
function is_active($user){
	$user = mysql_real_escape_string($user);
	
	$sql = "SELECT
				COUNT(`user_activations`.`user_id`)
			FROM `users`
			INNER JOIN `user_activations`
			ON `users`.`user_id`=`user_activations`.`user_id`
			WHERE `users`.`user_name` = '{$user}'";
			
	$result = mysql_query($sql);
	
	return (mysql_result($result, 0) == '0') ? true : false;
}

//activates the account related to the given activation code.
function activate_account($aid) {
    $aid = mysql_real_escape_string($aid);
           
    mysql_query("DELETE FROM `user_activations` WHERE `activation_code` = '{$aid}'");
}

//adds user to the database.
function add_user($user, $email, $pass){
	$user  = mysql_real_escape_string(htmlentities($user));
	$email = mysql_real_escape_string($email);
	$pass  = sha1($pass);
	
	$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
	$aid = implode('', array_rand($charset, 10));
	
	$body = <<<EMAIL
	
	Hi.
	Thanks for registering, before you login you need to activate your account.
	
	To do that, simply click the following link.
	
	D:/XAMPP/xampp/htdocs/Better/activate.php?aid={$aid}
	
	
EMAIL;

	mail($email, 'Your new account at CupidCity.com', $body, 'From: no-reply@cupidcity.com');
	
	mysql_query("INSERT INTO `users` (`user_name`, `user_password`,`user_email`) VALUES ('{$user}', '{$pass}', '{$email}')");
	
	$user_id = mysql_insert_id();
	
	mysql_query("INSERT INTO `user_activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");
}


?>
user.inc.php from user profiles:
<?php

//fetches all 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;
}

//fatches 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);
}

//updates the current user profile info.
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($location);
	
	$sql = "UPDATE `users` SET
				`user_email` = '{$email}',
				`user_about` = '{$about}',
				`user_location` = '{$location}'
			WHERE `user_id` = {$_SESSION['uid']}";
			
	mysql_query($sql);
}
?>

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 6:39 pm
by jacek
kalipsso wrote:does init.inc.php from user system conflicts wit the init.inc.php from the user profile ?
the point of the init file is that it does things that are always needed, so you should not have more than one init file.

Before the call to the fetch_user_info() function can you try var_dump($_SESSION['uid']) to see if that has the value that you expect ?

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 7:12 pm
by kalipsso
jacek wrote:
kalipsso wrote:does init.inc.php from user system conflicts wit the init.inc.php from the user profile ?
the point of the init file is that it does things that are always needed, so you should not have more than one init file.

Before the call to the fetch_user_info() function can you try var_dump($_SESSION['uid']) to see if that has the value that you expect ?
in what file to add that?
Thanks..

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 7:20 pm
by jacek
the profile page I guess, which ever you posted in the first post of this topic.

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 7:27 pm
by kalipsso
jacek wrote:the profile page I guess, which ever you posted in the first post of this topic.
That is the edit_profile.php file..

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 7:33 pm
by jacek
kalipsso wrote:That is the edit_profile.php file..
Okay, try it there then.

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 7:37 pm
by kalipsso
jacek wrote:
kalipsso wrote:That is the edit_profile.php file..
Okay, try it there then.
i get an error on the line i put the code...

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 7:40 pm
by kalipsso
OK.
I added this code in init.inc.php from user register/login system
$_SESSION['uid'] = 12;
This is the result:
Image
if i remove that code, the edit form is empty..

Re: User profile No Data Displayed

Posted: Fri Jun 10, 2011 11:07 pm
by jacek
you cant have output before the session_start function, you will have to add it after the include.

Re: User profile No Data Displayed

Posted: Sat Jun 11, 2011 12:06 pm
by kalipsso
jacek wrote:you cant have output before the session_start function, you will have to add it after the include.
I found the error source...
I had a space before
<?php include('core/init.inc.php'); ?>
But still...i cant make it reflect the logged in user details...
what to do?

Re: User profile No Data Displayed

Posted: Sat Jun 11, 2011 1:55 pm
by jacek
kalipsso wrote:what to do?
I answered this in your other topic

http://betterphp.co.uk/board/viewtopic. ... =306#p3180

Re: User profile No Data Displayed

Posted: Sat Jun 11, 2011 8:21 pm
by kalipsso
Alright.
i manage to connect the 2 scripts together..with no errors...