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.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.

