120 likes | 224 Views
Perl/cgi and an Access Database. Please use speaker notes for additional information!. <!bccstudentsA.html> <html> <head> <title>BCC Students</title> </head> <BODY> <h1>BCC Student eMail List</h1> <form action="bccstudentsA.cgi" method=post>
E N D
Perl/cgi and an Access Database Please use speaker notes for additional information!
<!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>
#!/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 Note that I have defined Print_Pg_Hdr which is a user-defined function that I can execute.
$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 trying 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 Note the punctuation when inserting SQL into Perl.
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