Modifying Users at the User Registration Website

 

Introduction.  Now we need to allow users to modify their previously recorded inputs on the web.  This approach makes use of three pages
  • an input form
    • requiring email address and password
  • a processing page that
    • validates the inputted email address and password
    • copies these into session variables for later use
    • obtains the user's previously inputted values if found
    • places these database values into a sticky form
  • another processing page that
    • updates the current inputs based on the modifications in the form
      • it identifies the record to be modified based on the original email address

The form page is quite straight forward with only two text fields.  This page should be called modify_user.html.

 

<html>
<head>
<title>Submission of Self Validation for Record Modification</title>
</head>

<body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
<form action="modify_user.jsp" 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 page should look like the following.

 

 

This page posts to another page.

The processing script first tests to see whether the entries are present in the table using an SQL SELECT command.  If they are then they are displayed in a form that can be modified.  If the inputs aren't present the user is informed of this.

You should call the page modify_user.jsp.

 

<html>
<head>
<title>Submission of Self Validation for Record Modification</title>
</head>
<%@ page import = "java.sql.*" %>
<body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
<%
// obtaining and prepping the input data from text boxes
String email = request.getParameter("txt_email");
if (email == null) email = "";

String str_password = request.getParameter("txt_password");
if (str_password == null) str_password = "";

// assign the values for database access
try
{

// assign the values for database access
Class.forName ("org.gjt.mm.mysql.Driver");

String url = "jdbc:mysql://localhost:3306/jasperat";
String username = "jasperat";
String password = "f5W2-Ld5s";

Connection con = DriverManager.getConnection ( url, username, password);

String qs = "SELECT * FROM yourlastname_user_registration WHERE (email = '" + email + "' AND password = '" + str_password + "')";
Statement stmt = con.createStatement( );

ResultSet retrieveSet = stmt.executeQuery( qs );

if (retrieveSet.next( ))
{

session.setAttribute("original_email", email);
session.setAttribute("original_password", password);
String first_name = retrieveSet.getString("first_name");
String last_name = retrieveSet.getString("last_name");
String php_interest = retrieveSet.getString("php_interest");
String jsp_interest = retrieveSet.getString("jsp_interest");
String mysql_interest = retrieveSet.getString("mysql_interest");
String credit_card = retrieveSet.getString("credit_card");
String education = retrieveSet.getString("education");
%>
<form action="update_user_registration.jsp" method=post>
<table>
<tr>
<td><font size = 4 color=cccccc>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20 value = "<%=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 = "<%=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 = "<%=email%>">
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Password:</font>
</td>
<td><input type=password name="txt_password" size=50 value = "<%=str_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="php_yes"
<% if (php_interest.equals("php_yes")) { %> CHECKED <% } %>>
<font size = 4 color=cccccc>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_jsp" value="jsp_yes"
<% if (jsp_interest.equals("jsp_yes")) { %> CHECKED <% } %>>
<font size = 4 color=cccccc>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_mysql" value="mysql_yes"
<% if (mysql_interest.equals("mysql_yes")) { %> 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" <% if (credit_card.equals("Discover")) out.print("selected"); %>>Discover
<option value="MasterCard" <% if (credit_card.equals("MasterCard")) out.print("selected"); %>>Mastercard
<option value="Visa" <% if (credit_card.equals("Visa")) out.print("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" <% if (education.equals("NoCollege")) out.print("checked"); %>><font size = 4 color=cccccc>No College</font><br>
<input type=radio name="rb_education" value="College" <% if (education.equals("College")) out.print("checked"); %>><font size = 4 color=cccccc>College</font><br>
<input type=radio name="rb_education" value="Masters" <% if (education.equals("Masters")) out.print("checked"); %>><font size = 4 color=cccccc>Masters</font><br>
<input type=radio name="rb_education" value="PhD" <% if (education.equals("PhD")) out.print("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>

<%

}
else
{

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

}
// closing the ResultSet, Statement and Connection
retrieveSet.close( );
stmt.close( );
con.close( );

}
// the catch block for the connection
catch (java.lang.Exception ex)

{

// Print description of the exception.
System.out.println( "** Error on data select. ** " );
ex.printStackTrace ( );

}
%>
</body>
</html>

 

You are likely to need to get the SQL statement string to be all on one line.

Notice the try and catch blocks.  These are usually required by the JDBC.

Notice how the retrieveSet.next( ) command is used to see if the SELECT query returned any results.

The form that gets displayed should look a lot like the following.

 

 

Now we need to process the inputs gathered from this form in order to update the user's information in the database.  This needs to be done in a page called update_user_registration.jsp.

 

<%@ page import = "java.sql.*" %>
<%
// putting the session variables containing the
// original email and password used to locate the record
// into local variables to be used in the SQL
String original_email = (String) session.getAttribute("original_email");
String original_password = (String) session.getAttribute("original_password");

// obtaining and prepping the input data from the preceding form
String first_name = request.getParameter("txt_first_name");
if (first_name == null) first_name = "";

String last_name = request.getParameter("txt_last_name");
if (last_name == null) last_name = "";

String email = request.getParameter("txt_email");
if (email == null) email = "";

String str_password = request.getParameter("txt_password");
if (str_password == null) str_password = "";

// obtaining and prepping the data from check boxes
String php_interest = request.getParameter("chk_php");
if (php_interest == null) php_interest = "php_no";

String jsp_interest = request.getParameter("chk_jsp");
if (jsp_interest == null) jsp_interest = "jsp_no";

String mysql_interest = request.getParameter("chk_mysql");
if (mysql_interest == null) mysql_interest = "mysql_no";

// obtaining and prepping the data from the select box
String credit_card = request.getParameter("sel_credit_card");
if (credit_card == null) credit_card = "";

// obtaining and prepping the data from the radio buttons
String education = request.getParameter("rb_education");
if (education == null) education = "";

// assign the values for database access
try
{

// assign the values for database access
Class.forName ("org.gjt.mm.mysql.Driver");

String url = "jdbc:mysql://localhost:3306/jasperat";
String username = "jasperat";
String password = "f5W2-Ld5s";

Connection con = DriverManager.getConnection ( url, username, password);
// constructing the query string
String qs = "UPDATE yourlastname_user_registration SET
first_name = '" + first_name +
"',last_name = '" + last_name +
"',email = '" + email +
"',php_interest = '" + php_interest +
"',jsp_interest = '" + jsp_interest +
"',mysql_interest = '" + mysql_interest +
"',credit_card = '" + credit_card +
"',education = '" + education +
"' WHERE (email = '" + original_email + "')";
Statement stmt = con.createStatement( );

stmt.executeQuery( qs );
// closing the Statement and Connection
stmt.close( );
con.close( )

}
// the catch block for the connection
catch (java.lang.Exception ex)

{

// Print description of the exception.
System.out.println( "** Error on data select. ** " );
ex.printStackTrace ( );

}
%>
<html>
<head>
<title>Updating the User Registraion Table in MySQL Using JSP</title>
</head>

<body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
<p align='center'><a href='user_registration_home.html'><font size = 4><b>User Home Page</b></font></a></p>
</body>
</html>

 

Again, as usual, the query string is likely to need to be all on one line.  I have broken it down across several lines in order to help clarify its contents.