Welcome To The Contact Database
"; echo "You have the following choices:"; echo "
"; echo " List Contacts.
"; echo "Create a new Contact.

"; echo ""; echo "

"; } /* List the first and last name of people in the contact database plus their phone number. Make the last name clickable for the full record. */ Function Show_Names (){ echo "

Contact List

"; mysql_connect($hosthame,$user,$password); $result = mysql("services","SELECT uid, last_name, first_name, FROM contacts ORDER BY last_name"); echo ""; $total_rows = mysql_numrows($result); $counter = 0; echo ""; while($counter < $total_rows) { $uid = mysql_result($result,$counter,"uid"); echo "
Last NameFirst NameWork Phone
\n"; echo ""; echo mysql_result($result,$counter,"last_name"); echo "\n"; echo "\n"; echo mysql_result($result,$counter,"first_name"); echo "\n"; $counter = $counter + 1; } echo "
"; } /* Save updates to database. Call Print_Contact to list new information for updated contact entry. */ Function Commit_Update (){ mysql_connect($hosthame,$user,$password); $result = mysql("services","UPDATE contacts SET first_name = '$fn', last_name = '$ln', phone = '$ph', fax = '$fx', email = '$em' WHERE uid = $uid"); $state = ""; Print_Contact($uid); } /* Save new contact information to database. Call Print_Contact to display new contact entry. */ Function Commit_Contact () { mysql_connect($hostname,$user,$password); $result = mysql("services","SELECT now()"); /* Get the Current Datetime */ /* Note that it would be better to use a field of type TIMESTAMP here, since it would automatically assign the current date and time. I haven't done that here since I wanted to demonstrate using functions in MySQL. */ $now = mysql_result($result,0,"now()"); $result = mysql("services","INSERT INTO contact (first_name, middle_initial,last_name, phone, fax, email) VALUES ('$fn', '$mi','$ln', '$ph', '$fx', '$em')"); $new_uid = mysql_insert_id(); $result = mysql("services","INSERT INTO comment (uid, contact_date, contact_comment) VALUES ($new_uid, '$now' , 'Contact Record Created')"); $state = ""; Print_Contact($new_uid); }; /* This function displays the contact information in a form. It is used for both the create and update contact options. */ Function Contact_Form (){ if($state == "Update"){ $state = "Commit_Update"; $result = mysql("services","SELECT * FROM contacts WHERE uid = $uid"); $last_name = mysql_result($result,0,"last_name"); $first_name = mysql_result($result,0,"first_name"); $middle_initial = mysql_result($result,0,"middle_initial"); $phone = mysql_result($result,0,"phone"); $fax = mysql_result($result,0,"fax"); $email = mysql_result($result,0,"email"); } else { $state = "Commit_Contact"; } echo "

Contact Form

\n"; echo "

\n"; echo " \n"; echo " \n"; echo " First Name:

\n"; echo " Middle Initial:

\n"; echo " Last Name:

\n"; echo " Phone:

\n"; echo " Fax:

\n"; echo " Email:

\n"; echo "

\n"; echo "
\n"; } /* Add a comment to the database. Call Print_Contact to display changes. */ Function Add_Comment (){ mysql_connect($hosthame,$user,$password); $result = mysql("services","SELECT now()"); /* Get the Current Datetime */ $now = mysql_result($result,0,"now()"); $result = mysql("services","INSERT INTO comment (uid, contact_date, contact_comment) VALUES ($uid, '$now', '$contact_comment')"); Print_Contact($uid); } /* Print address and comments for a contact. Also give option to update or add comments. */ Function Print_Contact (){ mysql_connect($hosthame,$user,$password); $result = mysql("services","SELECT * FROM contact WHERE uid = $uid"); $name = mysql_result($result,0,"first_name"); $name = $name + " " + mysql_result($result,0,"middle_initial"); $name = $name + " " + mysql_result($result,0,"last_name"); echo "Contact Information For $name"; echo ""; echo ""; echo "

Contact Information For $name

"; echo ""; echo ""; echo ""; $result = mysql("services","SELECT * FROM contact WHERE uid = $uid"); echo ""; echo "\n"; echo ""; echo "\n"; $email = mysql_result($result,0,"email"); echo ""; echo "\n"; echo "
Name$name
Phone"; echo mysql_result($result,0,"phone"); echo "
Fax"; echo mysql_result($result,0,"fax"); echo "
Email Address $email"; echo "
"; echo "
"; echo " \n"; echo " \n"; echo "
"; echo "


"; echo "

Comments For This Contact

"; echo "
"; $result = mysql("services","SELECT * FROM comment WHERE uid = $uid ORDER BY contact_date"); $total_rows = mysql_numrows($result); $counter = 0; echo ""; echo ""; while($counter < $total_rows) { echo ""; $counter = $counter + 1; } echo "
DateComment
"; echo mysql_result($result,$counter,"contact_date"); echo ""; echo mysql_result($result,$counter,"contact_comment"); echo "
"; echo "

Add new comment below."; echo "

"; echo "


"; echo "

"; echo "

"; echo "

"; echo "

"; echo "

"; echo "
"; echo "
\n"; } /* The main loop. Call functions based on the value of $state, which gets set via a hidden INPUT TYPE. */ switch($state) { case ""; Main_Menu(); break; case "List"; Show_Names(); break; case "Create"; Contact_Form($state, $uid); break; case "Update"; Contact_Form($state, $uid); break; case "Commit_Update"; Commit_Update($uid,$first_name,$middle_initial,$last_name,$phone,$fax,$email); break; case "Commit_Contact"; Commit_Contact($first_name,$middle_initial,$last_name,$phone,$fax,$email); break; case "Add_Comment"; Add_Comment($uid,$contact_comment); break; case "Print_Contact"; Print_Contact($uid); } ?>