Usersystem Part 2

Posted by Pete | Posted in PHP Tutorials | Posted on 04-10-2009-05-2008

0

Well the next part is available for use :) . This part of the usersystem is the next part to the part 1 which can be found here. In this part we will focus on creating a ranking system so we can distinguish who is an administrator and who is a member. We will also create a function so that updating a user’s details is simple. Once we have this function we will create a members list, profile page for each user, and allow the user to edit their profile and password. So lets get stuck in.

So lets get started first we need to add more fields into the users table. So what we need to do is run the following MySQL query in the users table.
ALTER TABLE `users` ADD `msn` VARCHAR( 90 ) NOT NULL AFTER `email` ,
ADD `yahoo` VARCHAR( 90 ) NOT NULL AFTER `msn` ,
ADD `aim` VARCHAR( 90 ) NOT NULL AFTER `yahoo` ,
ADD `interests` VARCHAR( 1000 ) NOT NULL AFTER `aim` ,
ADD `hobbies` VARCHAR( 1000 ) NOT NULL AFTER `interests`
ADD `level` VARCHAR( 2 ) NOT NULL AFTER `hobbies`

Now we have the database sorted. We will create the function to update the user details. So open functions.php and before ?> add the following

function update_user($user_id, $values = array()){
	$update_field = ""; // Start the $update_field so we can add to it.
	if(!is_array($values)){ // If $values isnt in an array show error.
		return "Update Values Not In An Array"; // Return the message.
	}else{ // Carry On :)
		$conn = mysql_connect(DATABASE_LOCATION,DATABASE_USERNAME,DATABASE_PASSWORD); // Connect to the mysql server
		mysql_select_db(DATABASE_NAME,$conn); // Select the database

		$values = protection($values);
		foreach ($values as $key => $value){ // Go through the array
			$get_columns = mysql_query("DESCRIBE `users`"); // Get the information about the users table.
			while($r=mysql_fetch_array($get_columns)) // Put the information into an array and go through it.
			{
				if($key == $r['Field']){ // Check if $key from the $values array is a valid database column.
					$update_field .= "`".$key."` = '".$value."', "; // If it is add it to $update_field variable.
					break; // Stop Loop.
				}
   			}
		}
		$update_field = substr($update_field, "0", "-2"); // Now there all added remove the last , and space.
   		$update = mysql_query("UPDATE `users` SET ".$update_field." WHERE `id` = '".$user_id."'"); // Update database
   		if($update){ // Check if it successfully updated
   			return "1"; // If it did return 1
   		}else{ // If not return Unknown Error.
   			return "Unknown Error";
   		}
	}
}

So now we can update the user with ease so lets now get started on editing your profile. For this we need to create a new page called editprofile.php






	




You must fill in an email address.
"; // If it is show error }else{ // Otherwise go on $update_array = array("email" => $_POST['email_address'], "msn" => $_POST['msn'], "yahoo" => $_POST['yahoo'], "aim" => $_POST['aim'], "interests" => $_POST['interests'], "hobbies" => $_POST['hobbies']); // Put all fields into an array for the update function below $update = update_user($loggedin['user_id'], $update_array); // Get the id and the array and send it to the update function. if($update == "1"){ // If successfully update show successfully updated else show error. echo "
Successfully Updated Profile.
"; }else{ echo "
".$update."
"; } } } ?>
Edit Your ProfileChange Password
Email Address: MSN: Yahoo: AIM: Interests: Hobbies:
You are not logged in.br /> Please login here

So you can now edit your profile but you still cant edit your password so lets create that next. Create another page with the following code and save it as changepassword.php






	




A Field Was Left Blank.
"; // If it is show error. } else { // Otherwise go on if (md5($_POST['current_pw']) != $loggedin['password']) // Encrypt current_pw from form and check if current password matches form. { echo "
Current Password Is Incorrect
"; // If not show error. } else { if ($_POST['new_pw'] != $_POST['confirm_pw']) // Check if the new passwords match { echo "
Passwords Do Not Match
"; // If not show error. } else { $update_array = array("password" => md5($_POST['new_pw'])); // Put new pw field into array $update = update_user($loggedin['user_id'], $update_array); // Get the user id and the array and send it to the update function. if ($update == "1") { // If successfully update show successfully updated else show error. echo "
Successfully Updated Password.
"; } else { echo "
" . $update . "
"; } } } } } ?>
Change Your Password
Current Password: New Password: Confirm Password:
You are not logged in.br /> Please login here

Finally we need to create the page where we can view all the members and their profiles. So create members.php with the following.






	





Members List
" . $mq['username'] . " - " . $mq['level'] . ""; // Echo data } ?>
You are not logged in.br /> Please login here

and finally profile.php






	





Profile
User ID: Username: Email Address: MSN: Yahoo: AIM: Level:
You are not logged in.br /> Please login here
Dont forget membersarea.php here we have multiple edits. In members area we will be adding a few links and also the ability to upgrade members to administrators. find
Welcome To The Members AreaLogout

and replace with

Welcome To The Members AreaLogout | Edit ProfileMembers List

This will add edit profile link and members list link.

find

		Email Address: 

after add

		MSN: 
		Yahoo: 
		AIM: 
		Level: 

find

and replace with

= "10"){ // Check if the user logged in is an administrator
	if($_POST['update_user_level']){
		$update_user_details = update_user($_POST['update_user'], array("level" => $_POST['user_level']));
		if($update_user_details){
			echo "Successfully updated user";
		}else{
			echo "Unknown Error.";
		}
	}
?>
Your an administrator :) 

So now we are finished for this part. I'm sorry about the CSS I'm not very good at CSS atm although im trying to learn it. If you have any questions about this part of the usersystem or like to leave some feedback please leave a comment.

Write a comment

You must be logged in to post a comment.

Tutorials Written By Peter Kelly