Modifying a User on a User Registration Website
Introduction.
As you should expect, I have left the most difficult functionality for
last. Hopefully, you will see the "wholes" or have some "gestalts"
that will help you work with the details and see the bigger picture.
Modifying a user's stats will require three pages
One of the other complications that occurs in this process is that the user might want to update their email address. Since the e-mail address is used to identify the user as they are currently registered before the update occurs we need to retain the email address they entered into the initial form through the entire process. I tried doing this with session variables and couldn't get it to work. What I ended up doing was posting this current or "old" email address to a hidden text box on the second page that processes the verification and displays the current entries. Then this value is posted through to the page that contains the UPDATE query and used in the query to identify the user in the database table. The first file is the form page for user inputs in order to verify who the yare and should be called modify_user.html. |
<html> <head> <title>HTML Form</title> </head> <body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799"> <form action="modify_user.php" method=post> <h2>Please enter the E-Mail Address and Password of the User</h2> <table> <tr> <td><font size = 4 color=cccccc>EMail Address:</font> </td> <td><input type=text name="txt_email" size=50> </td> </tr> <tr> <td><font size = 4 color=cccccc>Password:</font> </td> <td><input type=password name="txt_password" size=12> </td> </tr> <tr> <td colspan = 2 align = center><input type = submit name="submit" value="submit"> </td> </tr> </table> </form> </body> </html> |
The form is quite simple and should look like the following. |
Notice that this has text boxes for only an e-mail
address and password. Now we need the first processing script for verification including the form page that displays the current entries. This is a very typical use of a sticky form. In order to post properly from the initial form, you should call this modify_user.php. While it has very minimal input error trapping, it makes use of an SQL SELECT query to make certain there is such a record in the database. The ways to connect should look familiar. |
<html> <head> <title>HTML Form</title> </head> <body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799"> <?php // assign the values for database access $host = "localhost"; $user = "your_user_name"; $password = "your_password"; $db_name = "database_name"; $table_name = "user_registration"; // connecting to the database on battcave.com $link = mysql_connect($host, $user, $password); // constructing the query string $query_string = "SELECT * FROM $table_name WHERE (email = '$txt_email' AND password = '$txt_password')"; $result_set = mysql_db_query($db_name, $query_string, $link); // based on whether this query returns an entry we can // fill the form or tell them to try again if(mysql_num_rows($result_set) > 0) {
} // end if statement
} |
This performs the desired operations according to what
the user inputs and what is in the table. Notice all the PHP
code segments embedded within the different HTML form controls. You
should also notice the hidden text box, txt_old_email, that is being used
to pass the user's initial email address to the final processing script
page. The sticky form should look like the following for someone in the table. |
The final processing script won't be all that different from what we have seen other than we need to use a MySQL UPDATE statement in order to update a record that is already in the database table. You should call this page update_user_registration.php. |
<?php // obtaining and prepping the input data $txt_first_name = trim($txt_first_name); $txt_last_name = trim($txt_last_name); $txt_email = trim($txt_email); // ensuring the checkboxes have some value if ($chk_php != 1) $chk_php = ""; if ($chk_jsp != 1) $chk_jsp = ""; if ($chk_mysql != 1) $chk_mysql = ""; // assign the values for database access $host = "localhost"; $user = "desaighuweb"; $password = "globekid"; $db_name = "desaighuweb"; $table_name = "user_registration"; // connecting to the database on battcave.com $link = mysql_connect($host, $user, $password); // constructing the query string $query_string = "UPDATE $table_name SET first_name = '$txt_first_name', last_name = '$txt_last_name', email = '$txt_email', php_interest = '$chk_php', jsp_interest = '$chk_jsp', mysql_interest = '$chk_mysql', credit_card = '$sel_credit_card', education = '$rb_education' WHERE (email = '$txt_old_email')"; if (mysql_db_query($db_name, $query_string, $link)) {
}
} |
Hopefully, you won't get any problems due to missing
necessary spaces when copying this or any of the other code. Notice how the UPDATE query is based on the $txt_old_email in the WHERE clause. |