Deleting a User on a User Registration Website

 

Introduction.  Now that we have some users we need to be able to allow them to delete themselves. This will require two pages
  • a form page
  • a processing script

In addition, since we don't want just anyone deleting anyone they want we need to build in some sort of verification.  As stated in earlier pages we will base our verifications on a password and e-mail address.

The first file is the form page for user inputs and should be called delete_user.html.

 

<html>
<head>
<title>HTML Form</title>
</head>

<body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
<form action="delete_from_user_registration.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 the has text boxes for only an e-mail address and password.

Now we need the processing script.  In order to post properly from the form, you should call this delete_from_user_registration.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.  Then we will delete it accordingly.  The ways to connect should look familiar.

 

<html>
<head>
<title>Deleting a Record from a Table in MySQL Using PHP</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 to make sure the user exists
$find_string = "SELECT * FROM $table_name WHERE (email = '$txt_email' AND password = '$txt_password')";
$find_set = mysql_db_query($db_name, $find_string, $link);
// if the user exists then delete
// otherwise give appropriate feedback

if (mysql_num_rows($find_set) > 0)
{

// developing the query string to delete the appropriate user
$query_string = "DELETE FROM $table_name WHERE (email = '$txt_email' AND password = '$txt_password')";
$result_set = mysql_db_query($db_name, $query_string, $link);
print("<p align='center'><font size = 4>The deletion 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>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>");

}

mysql_close($link);
?>
</body>
</html>

 

This performs the desired operations according to what the user inputs and what is in the table.