// guestbook5.php3
//
// by Jeff Schmitt
// Towson University
// March, 1998
// June, 1998
//
// freely dapted from: http://infoband.com/timesheet/employee.php3
function tableentry($n,$a1,$a2,$e) {
if ($n) echo "$n<BR>\n";
if ($a1) echo "$a1<BR>\n";
if ($a2) echo "$a2<BR>\n";
if ($e) echo "$e<BR>\n";
echo "<HR NOSHADE>\n";
}
?>
<TITLE>Guest Book of Jeff Schmitt</TITLE>
</HEAD>
<BODY>
<H2>Please sign my guest book</H2>
<?
// Start out with some fairly static variables
$sDB = "schmitt";
$sTable = "guestbook5";
$sServer = ""; // null string is also called 'localhost'
if ($fp=fopen("/usr/faculty/schmitt/.mysql-init","r")) {
$sUser=chop(fgets($fp,500)); // chop -- remove trailing whitespace
$sPass=chop(fgets($fp,500));
fclose($fp);
} else {
echo "error -- cannot read .mysql-init";
exit;
}
// Establish a connection with the database server
if (!($nConnect=mysql_connect($sServer, $sUser, $sPass))) {
echo "Connect error -- the guestbook is not available.\n";
exit;
}
// Establish a connection with the database server
if (!mysql_select_db($sDB, $nConnect)) {
echo "database select error: $sDB -- the guestbook is not available.\n";
exit;
}
// When the user hits the 'Submit' button for the form to be displayed
// below, $action will be true and the following 'if' statement block will
// be executed. It will update any values the user changed.
//
// If you are looking at this for the first time, you might want to skip
// this 'if' block and see the form below to actually see what this does
// with the data from that form.
if ($action) {
// The user hit the update button.
// Now go through every field and perform neccessary actions
echo "DEBUG: action routine<BR>\n";
echo "<FONT COLOR=\"#3333FF\">\n";
// make sure the form is not all blank
if ($NAME || $AFF1 || $AFF2 || $EMAIL) {
if (mysql_query("INSERT INTO $sTable (name, aff1, aff2, email) ".
"VALUES ('$NAME', '$AFF1', '$AFF2', '$EMAIL')")) {
echo "Guestbook Succesfully Updated!";
} else {
echo "ERROR in insert";
}
} else {
echo "ERROR: blank form values -- ignored";
}
echo "</FONT><P>\n";
}
?>
<P><FONT SIZE="3">Current Guestbook:</FONT><BR>
<?
// List the current guestbook. The following uses
// the object class we built above. Refer to the
// comments in the class for descriptions of what each
// property and method is/does.
$sql = "SELECT * FROM $sTable";
if ($nResult=mysql_query($sql)) {
// fetch the row into an 'object' which has components
// for each of the columns of the currect row
while ($row=mysql_fetch_object($nResult)) {
tableentry($row->name,$row->aff1,$row->aff2,$row->email);
}
} else {
// if the select did work, we may need to create the table
$sql = "CREATE TABLE $sTable (name char(50), aff1 char(50), ".
"aff2 char(50), email char(50))";
if (mysql_query($sql)) {
echo "The guestbook database table has been created<BR>\n";
echo "Please try again<BR>\n";
} else {
echo "ERROR: command $sql<BR>\n";
}
}
?>
<FORM METHOD="POST" ACTION="<?echo $PHP_SELF;?>">
<TABLE BORDER=1>
<TR><TH>Name:<TD><INPUT NAME="NAME" size=50 MAXLENGTH=50>
<TR><TH>Affiliation:<TD><INPUT NAME="AFF1" size=50
MAXLENGTH=50><BR>
<INPUT NAME="AFF2" size=50 MAXLENGTH=50>
<TR><TH>E-mail:<TD><INPUT NAME="EMAIL" size=50
MAXLENGTH=50>
<TR><TD COLSPAN=4 ALIGN="CENTER">
<!-- By giving the submit button below a name attribute -->
<!-- ($action), we are able to check and determine if the user -->
<!-- just entered the script, or was here and hit the button -->
<!-- This is determined at the 'if ($action)' block above -->
<INPUT TYPE="Submit" NAME="action" VALUE="Submit">
</TR>
</TABLE>
</FORM>
<!-- Notice the form "ACTION" above is actually a short PHP script
that
// will 'echo' (or write, or print) a PHP variable called $PHP_SELF
// This variable will be the location of this file in respect to the
// virtual root of the web server. Basically, it submits the form
// to itself.
-->
</BODY>
</HTML>
| |