The HTML form will contain an input field for each column in the my_contacts table. In the previous chapter, you created 12 fields, which correspond to 12 columns. Your record-addition interface should have a space for each of these fields, except the ID field, which can be left blank.
| Note |
Because the ID field is an auto-incrementing field, if you add a record and leave the field blank, MySQL will place the next-highest available number in that field. |
Open a new file in your text editor and start a PHP block, and then start a session, or continue a session if it currently exists:
<? session_start();
Start an if…else block that checks the value of $_SESSION[valid] and performs a particular action based on the result. If the value is not yes, the user didn't go through the proper authentication channels:
if ($_SESSION[valid] != "yes") {
Send the user back to the login form, and exit this script:
header("Location: http://127.0.0.1/contact_menu.php");
exit;
Close your PHP block, and then type this HTML to start building the record-addition form:
Begin your form. Assume that the method is POST and the action is a script called do_addcontact.php:
<FORM METHOD="POST" ACTION="do_addcontact.php">
Begin an HTML table to assist in layout. Start a new table row, add two column headings, and then close that row:
<table cellspacing=3 cellpadding=5> <tr> <th>NAME & ADDRESS INFORMATION</th> <th>OTHER CONTACT/PERSONAL INFORMATION</th> </tr>
Start a new table row and table data cell, and then create an input field for the person's first name with a text label:
<tr> <td valign=top> <P><STRONG>First Name:</STRONG><BR> <INPUT TYPE="text" NAME="f_name" SIZE=35 MAXLENGTH=75></P>
In the same table data cell, create an input field for the person's last name with a text label:
<P><STRONG>Last Name:</STRONG><BR> <INPUT TYPE="text" NAME="l_name" SIZE=35 MAXLENGTH=75></P>
In the same table data cell, create an input field for the person's address (first line) with a text label:
<P><STRONG>Address Line 1:</STRONG><BR> <INPUT TYPE="text" NAME="address1" SIZE=35 MAXLENGTH=100></P>
In the same table data cell, create an input field for the person's address (second line) with a text label:
<P><STRONG>Address Line 2:</STRONG><BR> <INPUT TYPE="text" NAME="address2" SIZE=35 MAXLENGTH=100></P>
In the same table data cell, create an input field for the person's address (third line) with a text label:
In the same table data cell, create an input field for the person's ZIP/postal code with a text label:
<P><STRONG>Zip/Postal Code:</STRONG><BR> <INPUT TYPE="text" NAME="postcode" SIZE=35 MAXLENGTH=25></P>
In the same table data cell, create an input field for the person's country with a text label. Close the table data cell after this input field:
<P><STRONG>Country:</STRONG><BR> <INPUT TYPE="text" NAME="country" SIZE=35 MAXLENGTH=100></P> </td>
In a new table data cell, create an input field for the person's primary telephone number with a text label:
<td valign=top> <P><STRONG>Primary Telephone Number:</STRONG><BR> <INPUT TYPE="text" NAME="prim_tel" SIZE=35 MAXLENGTH=35></P>
In the same table data cell, create an input field for the person's secondary telephone number with a text label:
<P><STRONG>Secondary Telephone Number:</STRONG><BR> <INPUT TYPE="text" NAME="sec_tel" SIZE=35 MAXLENGTH=35></P>
In the same table data cell, create an input field for the person's e-mail address with a text label:
<P><STRONG>E-mail Address:</STRONG><BR> <INPUT TYPE="text" NAME="email" SIZE=35 MAXLENGTH=100></P>
In the same table data cell, create an input field for the person's birthday with a text label. Close the table data cell and the table row after this input field:
<P><STRONG>Birthday (YYYY-MM-DD):</STRONG><BR> <INPUT TYPE="text" NAME="birthday" SIZE=10 MAXLENGTH=10></P> </td> </tr>
| Note |
The date type used in MySQL uses the YYYY-MM-DD format. An example of a date using this format is 2004-03-20 (March 20, 2004). In this example application, your date-related form fields are designed for you to enter the dates manually, in this format. In Appendix C, "Writing Your Own Functions and Objects," you learn how to create code snippets that enable you to create dynamic lists for things like months, days, and years. |
Start a new table row and table data cell that spans two columns. Inside, add a submit button as well as a link back to the main menu. Close the table data cell, the table row, and the table itself:
<tr> <td align=center colspan=2><br> <P><INPUT TYPE="SUBMIT" NAME="submit" VALUE="Add Contact to System"></P> <p><a href="contact_menu.php">Return to Main Menu</a></p> </TD> TR> </TABLE>
Close your form and add some more HTML so that the document is valid:
</FORM> </BODY> </HTML>
Save the file with the name show_addcontact.php, and then place this file in the document root of your web server.
Your code should look something like this:
<?
//start a session
session_start();
//validate user to see if they are allowed to be here
if ($_SESSION[valid] != "yes") {
header("Location: http://127.0.0.1/contact_menu.php");
exit;
}
?>
<HTML>
<HEAD>
<TITLE>My Contact Management System: Add a Contact</TITLE>
</HEAD>
<BODY>
<h1>My Contact Management System</h1>
<h2><em>Add a Contact</em></h2>
<FORM METHOD="post" ACTION="do_addcontact.php">
<table cellspacing=2 cellpadding=1>
<tr>
<th>NAME & ADDRESS INFORMATION</th>
<th>OTHER CONTACT/PERSONAL INFORMATION</th>
</tr>
<tr>
<td valign=top>
<P><STRONG>First Name:</STRONG><BR>
<INPUT TYPE="text" NAME="f_name" SIZE=35 MAXLENGTH=75></P>
<P><STRONG>Last Name:</STRONG><BR>
<INPUT TYPE="text" NAME="l_name" SIZE=35 MAXLENGTH=75></P>
<P><STRONG>Address Line 1:</STRONG><BR>
<INPUT TYPE="text" NAME="address1" SIZE=35 MAXLENGTH=100></P>
<P><STRONG>Address Line 2:</STRONG><BR>
<INPUT TYPE="text" NAME="address2" SIZE=35 MAXLENGTH=100></P>
<P><STRONG>Address Line 3:</STRONG><BR>
<INPUT TYPE="text" NAME="address3" SIZE=35 MAXLENGTH=100></P>
<P><STRONG>Zip/Postal Code:</STRONG><BR>
<INPUT TYPE="text" NAME="postcode" SIZE=35 MAXLENGTH=25></P>
<P><STRONG>Country:</STRONG><BR>
<INPUT TYPE="text" NAME="country" SIZE=35 MAXLENGTH=100></P>
</td>
<td valign=top>
<P><STRONG>Primary Telephone Number:</STRONG><BR>
<INPUT TYPE="text" NAME="prim_tel" SIZE=35 MAXLENGTH=35></P>
<P><STRONG>Secondary Telephone Number:</STRONG><BR>
<INPUT TYPE="text" NAME="sec_tel" SIZE=35 MAXLENGTH=35></P>
<P><STRONG>E-mail Address:</STRONG><BR>
<INPUT TYPE="text" NAME="email" SIZE=35 MAXLENGTH=100></P>
<P><STRONG>Birthday (YYYY-MM-DD):</STRONG><BR>
<INPUT TYPE="text" NAME="birthday" SIZE=10 MAXLENGTH=10></P>
</td>
</tr>
<tr>
<td align=center colspan=2><br><br>
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="Add Contact to System">
<p><a href="contact_menu.php">Return to Main Menu</a></p>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
In the next section, you create the script that takes the form input, creates a SQL statement, and adds the record to the database table.