Mark's Attempt at PHP Database
/* Script Name: contact2.phtml
Script Purpose: Simple Contact Database Script Version: 1.1
Script Author: Michael J. Miller Jr. (mke@netcom.com)
*/
/* First let's set some variables. */
$hostname = "luna";
$password = "fzr400dead";
$user = "maggot";
/* Now to define functions. */
/* The main screen. Printed when $state is empty. */
Function Main_Menu (){
echo " Welcome to the Service Provider Database
";
echo "You have the following choices:";
echo "";
}
/* List the organizations in the contact database plus their
phone number. Make the last name clickable for the full record. */
Function Show_Records (){
echo "Service Providers
";
mysql_connect("luna", "maggot", "fzr400dead");
$result = mysql("services","SELECT * FROM organization");
echo "
";
$total_rows = mysql_numrows($result);
$counter = 0;
echo "| Service Provider | | |
";
while($counter < $total_rows) {
$uid = mysql_result($result,$counter,uid);
echo "| \n";
echo "";
echo mysql_result($result,$counter,org);
echo "\n";
echo " | \n";
echo mysql_result($result,$counter,uid);
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("luna","maggot","fzr400dead");
$result = mysql("services","UPDATE organization SET org = '$org', phone = '$phone', fax = '$fax',
email = '$email', address_1 = '$address1', address_2 = '$address2', URL = '$URL' WHERE uid = 1");
$state = "";
Print_Contact($uid);
}
/* Save new contact information to database. Call Print_Contact to display
new contact entry. */
Function Commit_Contact () {
mysql_connect("luna","maggot","fzr400dead");
$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 organization (org,phone, fax, email, address_1, address_2, URL) VALUES ('$org', '$phone', '$fax', '$email', '$address1', '$address2', '$URL')");
$new_uid = mysql_insert_id();
$result = mysql("services","INSERT INTO service (uid, entry_date, service) VALUES (1, '$now' , '$sevice')");
$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 organization WHERE uid=1");
$org = mysql_result($result,0,"org");
$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";
}
/* Add a comment to the database. Call Print_Contact to display changes. */
Function Add_Service (){
mysql_connect("luna","maggot","fzr400dead");
$result = mysql("services","SELECT now()");
/* Get the Current Datetime */
$now = mysql_result($result,0,"now()");
$result = mysql("services","INSERT INTO service (uid, entry_date, service) VALUES ($uid, '$now', '$newservice')");
Print_Contact($uid);
}
/* Print address and comments for a contact. Also give option to update
or add comments. */
Function Print_Contact (){
mysql_connect("luna","maggot","fzr400dead");
$result = mysql("services","SELECT * FROM contacts WHERE uid=1");
$name = mysql_result($result,0,first_name);
$name = $name . " " . mysql_result($result,0,last_name);
echo "Contact Information For $name"; echo "";
echo "";
echo "
Contact Information For $name
";
echo "
";
echo "| Name | ";
echo "$name |
";
mysql_connect("luna","maggot","fzr400dead");
$result = mysql("services","SELECT * FROM organization WHERE uid=1");
echo "| Phone | ";
echo "";
echo mysql_result($result,0,"phone");
echo " |
\n";
echo "| Fax | ";
echo "";
echo mysql_result($result,0,"fax");
echo " |
\n";
$email = mysql_result($result,0,"email");
echo "| Email Address | ";
echo " $email";
echo " |
\n";
echo "
";
echo "
";
$result = mysql("services","SELECT * FROM service WHERE uid=1");
$total_rows = mysql_numrows($result);
$counter = 0;
echo "
"; echo "| Date | Comment | ";
while($counter < $total_rows) {
echo "
| ";
echo mysql_result($result,$counter,"entry_date");
echo " | ";
echo mysql_result($result,$counter,"service");
echo " |
";
$counter = $counter + 1;
}
echo "
";
echo "
Add new service below.";
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 "Show Records";
Show_Records();
break;
case "Add New Record";
Contact_Form($state, $uid);
break;
case "Update";
Contact_Form($state, $uid);
break;
case "Commit_Update";
Commit_Update($uid,$org,$phone,$fax,$email,$address1,$address2,$URL);
break;
case "Commit_Contact";
Commit_Contact($uid,$org,$phone,$fax,$email,$address1,$address2,$URL);
break;
case "Add_Service";
Add_Service($uid,$newservice);
break;
case "Print_Contact";
Print_Contact($uid);
}
?>