Adding a User on a User Registration Website

 

Introduction.  Since we can't really do anything until we have some users on our website we first need to add some users.  This will require two pages
  • a form page
  • a processing script

Since MySQL doesn't yet have stored procedures we will create our operating queries within strings like we did in the previous example where we created the table for our data.

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

 

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

<body bgcolor = "003344" text="cccccc" link="00aacc" vlink="007799">
<form action="write_to_user_registration.php" method=post>
<table>
<tr>
<td><font size = 4 color=cccccc>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20>
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Last Name:</font>
</td>
<td><input type=text name="txt_last_name" size=20>
</td>
</tr>
<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=50>
</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><font size = 4 color=cccccc>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_jsp" value=1><font size = 4 color=cccccc>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_mysql" value=1><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">Discover
<option value="MasterCard">Mastercard
<option value="Visa">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"><font size = 4 color=cccccc>No College</font><br>
<input type=radio name="rb_education" value="College"><font size = 4 color=cccccc>College</font><br>
<input type=radio name="rb_education" value="Masters"><font size = 4 color=cccccc>Masters</font><br>
<input type=radio name="rb_education" value="PhD"><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>
</body>
</html>

 

The form should look like the following.

 

 

Notice that the form makes use of all of the basic form controls so that you will learn how to work with all of them.  But, it doesn't seem worth it to me to increase the code by just increasing the amount of information wee gather, so this is a pretty minimalist form.

Now we need the processing script.  In order to post properly form the form, you should call this write_to_user_registration.php.  While it has very minimal input error trapping, which will be left for your homework, it makes use of an SQL INSERT query to put the record into the database.  The ways to connect should look familiar.

 

<?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 = "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 = "INSERT INTO $table_name
VALUES ('0', '$txt_first_name', '$txt_last_name', '$txt_email', '$txt_password',
'$chk_php', '$chk_jsp', '$chk_mysql', '$sel_credit_card', '$rb_education', Now( ))";

if (mysql_db_query($db_name, $query_string, $link))
{

print("<p align='center'><font size = 4>The addition 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 addition 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>Writing to a Table in MySQL Using PHP</title>
</head>

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

 

The code is really quite minimal since the query does the  work.  You will get some errors if you try to leave some the entries blank since the fields were specified to be NOT NULL.

The trickiest thing to figure out was how to get blanks for the check boxes to be entered into the table so they could be retrieved in order to correspond to unchecked boxes.