email |
name |
major |
option |
arichards@yahoo.com |
Al Richards |
CIS |
CIX |
chersey@yahoo.com |
Carl Hersey |
CIS |
CII |
james@yahoo.com |
Jennifer Ames |
CIS |
CII |
jdoe@yahoo.com |
John Doe |
CIS |
CIM |
lhiggins@yahoo.com |
Linda Higgins |
CIS |
CIC |
sdaniels@yahoo.com |
Stephen
Daniels |
CIS |
CIW |
sgrove@yahoo.com |
Sarah Grove |
CIS |
CIW |
These
are the records in the database that we are using - or at least at one point
these were the records. I have done
some updating of the table.
Here
we are using the database type in your book.
bccstudents1.html
<!bccstudents1.html>
<html>
<head>
<title>BCC
Students</title>
</head>
<BODY>
<h1>BCC
Student eMail List</h1>
<form
action="http://www.pgrocer.com/cgi-bin/db/bccstudents1.cgi"
method=post>
The CIS Department
is publishing a newsletter to keep students up-to-date with changes that
are being made. This page will give you the opportunity to
sign up for the newsletter or
remove your name
from the newsletter list.<br><br>
Student Name:
<input type=text
name=stuname size=25><br>
Student Major:
<input type=text
name=major size=3><br>
Student Option:
<input type=text
name=option size=3><br>
eMail address:
<input type=text
name=email size=50><br><br>
<input
type=submit name=button value="Add me to the CIS Mailing List">
<input
type=submit name=button value="Remove me from the CIS Mailing
List"><br><br>
<input
type=submit name=button value="View my information">
<input
type=reset value="Reset">
</form>
</body>
</html>
bccstudents1.cgi
#!/usr/bin/perl
#bccstudents1.cgi -
add and remove names from CIS mailing list
print
"Content-type: text/html\n\n";
use CGI::Carp
qw(fatalsToBrowser);
use CGI
qw(:standard);
use SDBM_File;
use Fcntl;
use strict;
#declare variables
my ($button, $name,
$major, $option, $email);
#assign values to
variables
$button =
param('button');
$name =
param('stuname');
$major =
param('major');
$option =
param('option');
$email =
param('email');
if ($button eq
"Add me to the CIS Mailing List") {
add();
}
elsif ($button eq
"Remove me from the CIS Mailing List") {
remove();
}
elsif ($button eq
"View my information") {
view()
}
exit;
#*****user-defined
functions*****
sub view {
#declare variable
my %mail;
#open database, view record, close
database
tie(%mail, "SDBM_File",
"cislist", O_RDONLY, 0666)
or
die "Error opening cislist. $!, stopped";
($name, $major, $option) = split(/,/,
$mail{$email});
if (exists($mail{$email})) {
untie(%mail);
#create Web page
print "<html>\n";
print "<head><title>CIS Newsletter Mailing
List</title><basefont size=5></head>\n";
print "<h1>CIS Newsletter Mailing List</h1>\n";
print "<form
action='http://www.pgrocer.com/cgi-bin/db/bccstudents1a.cgi'
method=post>\n";
print "Student Name: <input
type=text name=stuname value='$name' size=25><br>\n";
print "Student Major: <input
type=text name=major value='$major' size=3><br>\n";
print "Student Option:
<input type=text name=option value='$option' size=3><br>\n";
print "eMail address: <input
type=text name=email value=$email size=50><br><br>\n";
print "<input type=submit
name=button value='Update Mailing List'><br><br>\n";
print "<a
href='http://www.pgrocer.com/db/bccstudents1.html'>OR...Click here to return
to main page</a>\n";
print "</body></html>\n";
}
else
{
untie(%mail);
#create Web page
print "<html>\n";
print "<head><title>CIS Newsletter Mailing
List</title><basefont size=5></head>\n";
print "<h1>CIS Newsletter Mailing List</h1>\n";
print "Record does not
exist.<br><br>\n";
print "<a
href='http://www.pgrocer.com/db/bccstudents1.html'>Click here to return to
main page</a>\n";
print "</body></html>\n";
}
} #endview
sub add {
#declare variable
my %mail;
#open database, add record, close database
tie(%mail, "SDBM_File",
"cislist", O_CREAT|O_RDWR, 0666)
or
die "Error opening cislist. $!, stopped";
$mail{$email} =
"$name,$major,$option";
untie(%mail);
#create Web page
print "<html>\n";
print "<head><title>CIS
Newsletter Mailing List</title><basefont
size=5></head>\n";
print "<h1>CIS Newsletter
Mailing List</h1>\n";
print "You are on our list to receive
your newsletter at $email.<br><br>\n";
print "<a
href='http://www.pgrocer.com/db/bccstudents1.html'>Click here to return to
main page</a>\n";
print
"</body></html>\n";
} #end add
sub remove {
#declare variables
my (%mail, $msg);
#open database
tie(%mail, "SDBM_File",
"cislist", O_RDWR, 0)
or
die "Error opening cislist. $!, stopped";
#determine if user's information is in the
database
if (exists($mail{$email})) {
delete($mail{$email});
$msg
= "Mail will not be sent to $email";
}
else {
$msg
= "Our mailing list does not include $email.";
}
#close database
untie(%mail);
#create Web page
print "<html>\n";
print "<head><title>CIS
Newsletter Mailing List</title><basefont
size=5></head>\n";
print "<h1>CIS Newsletter
Mailing List</h1>\n";
print
"$msg<br><br>\n";
print "<a
href='http://www.pgrocer.com/db/bccstudents1.html'>Click here to return to
main page</a>\n";
print
"</body></html>\n";
} #end remove
bccstudents1a.cgi
#!/usr/bin/perl
#bccstudents1a.cgi -
change names from CIS mailing list
print
"Content-type: text/html\n\n";
use CGI::Carp
qw(fatalsToBrowser);
use CGI
qw(:standard);
use SDBM_File;
use Fcntl;
use strict;
#declare variables
my ($button, $name,
$major, $option, $email);
#assign values to
variables
$button =
param('button');
$name =
param('stuname');
$major =
param('major');
$option = param('option');
$email =
param('email');
if ($button eq
"Update Mailing List") {
change();
}
exit;
#*****user-defined
functions*****
sub change {
#declare variable
my %mail;
#open database, add record, close database
tie(%mail, "SDBM_File",
"cislist", O_CREAT|O_RDWR, 0666)
or
die "Error opening cislist. $!, stopped";
$mail{$email} =
"$name,$major,$option";
untie(%mail);
#create Web page
print "<html>\n";
print "<head><title>CIS
Newsletter Mailing List</title><basefont
size=5></head>\n";
print "<h1>CIS Newsletter
Mailing List</h1>\n";
print "The information has been
changed.<br>\n";
print "Name:
$name<br>\n";
print "Major:
$major<br>\n";
print "Option:
$option<br>\n";
print "eMail:
$email<br><br><br>\n";
print "<a
href='http://www.pgrocer.com/db/bccstudents1.html'>Click here to return to
main page</a>\n";
print
"</body></html>\n";
} #end change
Now
we are going to use an Access database - basically the same records. These are located on www.pgrocer.biz
bccstudentsA.html
<!bccstudentsA.html>
<html>
<head>
<title>BCC
Students</title>
</head>
<BODY>
<h1>BCC
Student eMail List</h1>
<form
action="bccstudentsA.cgi" method=post>
The CIS Department
is publishing a newsletter to keep students up-to-date with changes that
are being made. This page will give you the opportunity to
sign up for the newsletter or
remove your name
from the newsletter list.<br><br>
Student Name:
<input type=text
name=name size=25><br>
Student Major:
<input type=text
name=major size=3><br>
Student Option:
<input type=text
name=option size=3><br>
eMail address:
<input type=text
name=email size=50><br><br>
<input
type=submit name=button value="Add me to the CIS Mailing List">
<input
type=submit name=button value="Remove me from the CIS Mailing
List">
<input
type=submit name=button value="View my information">
<input
type=reset value="Reset">
</form>
</body>
</html>
bccstudentsA.cgi
#!/usr/bin/perl
#bccstudentsA.cgi -
add and remove names from CIS mailing list
print
"Content-type: text/html\n\n";
use CGI qw(:standard
-debug);
use CGI::Carp
qw(fatalsToBrowser);
use Win32::ODBC;
use strict;
#declare variables
my ($button, $name,
$major, $option, $email, $dbh, $sqlstmt, %dbrow);
my ($ErrNum,
$ErrText, $ErrConn);
#assign values to
variables
$button =
param('button');
$name =
param('name');
$major =
param('major');
$option =
param('option');
$email =
param('email');
if ($button eq
"Add me to the CIS Mailing List") {
add();
}
elsif ($button eq
"Remove me from the CIS Mailing List") {
remove();
}
elsif ($button eq
"View my information") {
view()
}
exit;
#*****user-defined
functions*****
sub view {
#declare variable
my $msg = "";
#open database, read and display the
record, close database
$dbh = new
Win32::ODBC("general") or
die( "Failed trying to
connect: " );
$sqlstmt = "SELECT * FROM
emailtable WHERE email = '$email'" ;
$dbh->Sql("$sqlstmt");
$dbh->FetchRow();
%dbrow = $dbh->DataHash();
if
( ! $dbrow{ 'email' } == "" )
{
#create
Web page
Print_Pg_Hdr();
print "Name: $dbrow{ 'name' }<br>\n";
print "Major: $dbrow{
'major' }<br>\n";
print "Option: $dbrow{
'option' }<br>\n";
print "eMail: $dbrow{ 'email'
}<br><br>\n";
print
"</body></html>\n";
}
else
{
$msg
= "$email is not on our list. ";
Print_Pg_Hdr();
print
"$msg <br>\n";
print
"</body></html>\n";
}
$dbh->Close();
} #endview
sub add {
#declare variable
my $msg = "";
#open database, add record, close database
$dbh = new
Win32::ODBC("general") or
die( "Failed trying to
connect: " );
$sqlstmt = "INSERT INTO
emailtable VALUES ('$email', '$name', '$major', '$option');";
$dbh->Sql("$sqlstmt");
($ErrNum, $ErrText, $ErrConn) =
$dbh->Error();
if ($ErrNum)
{
if
($ErrNum == -1605)
{
$msg
= "$email is already on our list. ";
}
else
{
$msg
= "Undefined Database error while tryying to INSERT $email. ";
}
Print_Pg_Hdr();
print
"$msg <br>\n";
print
"</body></html>\n";
}
else
{
#create
Web page
Print_Pg_Hdr();
print
"You are on our list to receive your newsletter at
$email.<br><br>\n";
print
"</body></html>\n";
}
$dbh->Close();
} #end add
sub remove {
#declare variables
my $msg = "";
#open database
$dbh = new
Win32::ODBC("general") or
die( "Failed trying to
connect: " );
$sqlstmt = "SELECT * FROM
emailtable WHERE email = '$email'" ;
$dbh->Sql("$sqlstmt");
$dbh->FetchRow();
%dbrow = $dbh->DataHash();
if
( ! $dbrow{ 'email' } == "" )
{
$sqlstmt = "DELETE FROM emailtable WHERE email =
'$email';";
$dbh->Sql("$sqlstmt");
#create
Web page
$msg
= $email . " has been deleted from our mailing list.";
Print_Pg_Hdr();
print
"$msg<br><br>\n";
print
"</body></html>\n";
}
else
{
$msg
= "$email is not on our list.";
Print_Pg_Hdr();
print
"$msg <br>\n";
print
"</body></html>\n";
}
$dbh->Close();
} #end remove
sub Print_Pg_Hdr {
print "<html>\n";
print "<head><title>CIS
Newsletter Mailing List</title><basefont
size=5></head>\n";
print
"<body>\n";
print "<h1>CIS Newsletter
Mailing List</h1>\n";
} #end Print_Pg_Hdr