Deleting Users at the User Registration Website

 

Introduction.  Now we need to allow users to delete themselves from the web.  This approach makes use of two pages
  • an input form
    • requiring email address and password
  • a processing page that
    • validates the inputted email address and password
    • deletes the user with these values if found
    • informs the user if these values are not found

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

 

<html>
<head>
<title>HTML Form for Deleting User</title>
</head>

<body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
<form action="delete_from_user_registration.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 an SQL DELETE command is used to perform the deletion.  If the inputs aren't present the user is informed of this.

You should call the page delete_from_user_registration.jsp.

 

<html>
<head>
<title>Deleting a Record from a Table in MySQL Using PHP</title>
</head>
<%@ page import = "java.sql.*" %>
<%
// obtaining the email and password from the form
String email = request.getParameter("txt_email");
if (email == null) email = "";

String str_password = request.getParameter("txt_password");
if (str_password == null) str_password = "";
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 fs = "SELECT * FROM yourlastname_user_registration WHERE (email = '" + email +"' AND password = '" + str_password + "')";
Statement stmt = con.createStatement( );

ResultSet findSet = stmt.executeQuery( fs );

// if the user exists then delete
// otherwise give appropriate feedback
if (findSet.next( ))
{

// developing the query string to delete the appropriate user
String ds = "DELETE FROM yourlastname_user_registration WHERE (email = '" + email +"' AND password = '" + str_password + "')";
stmt.executeQuery(ds);
out.println("<p align='center'><font size = 4>The deletion has been completed</font></p>");
out.println("<p align='center'><a href='user_registration_home.html'><font size = 4><b>User Home Page</b></font></a></p>");

}
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
findSet.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 bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
</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.

The page provides links back to the User Registration HomePage.

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