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
  • a very small form page to collect the user's e-mail and password
  • a processing page that also has another form that
    • verifies the user's inputs
    • displays the current entries in the table for the user
  • a processing page to perform the modifications using an UPDATE query

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)
{

while ($row = mysql_fetch_array($result_set))
{

?>
<form action="update_user_registration.php" method=post>
<input type=hidden name="txt_old_email" size=20 value = "<?php echo $txt_email; ?>">
<table>
<tr>
<td><font size = 4 color=cccccc>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20 value = "<?php echo $row[first_name]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Last Name:</font>
</td>
<td><input type=text name="txt_last_name" size=20 value = "<?php echo $row[last_name]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>EMail Address:</font>
</td>
<td><input type=text name="txt_email" size=50 value = "<?php echo $row[email]; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Password:</font>
</td>
<td><input type=password name="txt_password" size=50 value = "<?php echo $row[password]; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Interests:</font>
</td>
<td><input type=checkbox name="chk_php" value=1
<?php if (($row[php_interest]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=cccccc>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_jsp" value=1
<?php if (($row[jsp_interest]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=cccccc>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_mysql" value=1
<?php if (($row[mysql_interest]) == 1 ) echo 'checked'; ?>>
<font size = 4 color=cccccc>MySQL</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Credit Card:</font>
</td>
<td><select name="sel_credit_card">
<option value="Discover" <?php if (($row[credit_card]) == 'Discover') echo 'selected'; ?>>Discover
<option value="MasterCard" <?php if (($row[credit_card]) == 'MasterCard') echo 'selected'; ?>>Mastercard
<option value="Visa" <?php if (($row[credit_card]) == 'Visa') echo 'selected'; ?>>Visa
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Education:</font>
</td>
<td><input type=radio name="rb_education" value="NoCollege"
<?php if (($row[education]) == 'NoCollege') echo 'checked'; ?>>
<font size = 4 color=cccccc>No College</font><br>
<input type=radio name="rb_education" value="College"
<?php if (($row[education]) == 'College') echo 'checked'; ?>>
<font size = 4 color=cccccc>College</font><br>
<input type=radio name="rb_education" value="Masters"
<?php if (($row[education]) == 'Masters') echo 'checked'; ?>>
<font size = 4 color=cccccc>Masters</font><br>
<input type=radio name="rb_education" value="PhD"
<?php if (($row[education]) == 'PhD') echo 'checked'; ?>>
<font size = 4 color=cccccc>Ph.D.</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td colspan = 2 align = center><input type = submit name="submit" value="submit">
</td>
</tr>
</table>
</form>
<?php

} // end while loop
mysql_close($link);

} // end if statement
else
{

print("<p align='center'><font size = 4>No records matched your inputs</font></p>");
print("<p align='center'><font size = 4>Please click on back to return to the form<BR>or click on the following link</font></p>");
print("<p align='center'><a href='user_registration_home.html'><font size = 4><b>User Home Page</b></font></a></p>");

}
?>
</body>
</html>

 

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))
{

print("<p align='center'><font size = 4>The update has been completed</font></p>");
print("<p align='center'><a href='user_registration_home.html'><font size = 4><b>User Home Page</b></font></a></p>");

}
else
{

print("<p align='center'><font size = 4>The update could not be completed</font></p>");
print("<p align='center'><a href='user_registration_home.html'><font size = 4><b>User Home Page</b></font></a></p>");

}

mysql_close($link);
?>
<html>
<head>
<title>Updating the User Registraion Table in MySQL Using PHP</title>
</head>

<body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
</body>
</html>

 

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.