Perl/cgi and an Access Database Please use speaker notes for additional information!
BCC Students BCC Student List 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. Student Name: Student Major: Student Option: address:
#!/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, $ , $dbh, $sqlstmt, %dbrow); my ($ErrNum, $ErrText, $ErrConn); #assign values to variables $button = param('button'); $name = param('name'); $major = param('major'); $option = param('option'); $ = param(' '); 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 table WHERE = '$ '" ; $dbh->Sql("$sqlstmt"); $dbh->FetchRow(); %dbrow = $dbh->DataHash(); if ( ! $dbrow{ ' ' } == "" ) { #create Web page Print_Pg_Hdr(); print "Name: $dbrow{ 'name' } \n"; print "Major: $dbrow{ 'major' } \n"; print "Option: $dbrow{ 'option' } \n"; print " $dbrow{ ' ' } \n"; print " \n"; } else { $msg = "$ is not on our list. "; Print_Pg_Hdr(); print "$msg \n"; print " \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 table WHERE = '$ '" ; $dbh->Sql("$sqlstmt"); $dbh->FetchRow(); %dbrow = $dbh->DataHash(); if ( ! $dbrow{ ' ' } == "" ) { #create Web page Print_Pg_Hdr(); print "Name: $dbrow{ 'name' } \n”; print "Major: $dbrow{ 'major' } \n"; print "Option: $dbrow{ 'option' } \n"; print " $dbrow{ ' ' } \n"; print " \n"; } else { $msg = "$ is not on our list. "; Print_Pg_Hdr(); print "$msg \n"; print " \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 table VALUES ('$ ', '$name', '$major', '$option');"; $dbh->Sql("$sqlstmt"); ($ErrNum, $ErrText, $ErrConn) = $dbh->Error(); if ($ErrNum) { if ($ErrNum == -1605) { $msg = "$ is already on our list. "; } else { $msg = "Undefined Database error while trying to INSERT $ . "; } Print_Pg_Hdr(); print "$msg \n"; print " \n"; } else { #create Web page Print_Pg_Hdr(); print "You are on our list to receive your newsletter at $ . \n"; print " \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 table WHERE = '$ '" ; $dbh->Sql("$sqlstmt"); $dbh->FetchRow(); %dbrow = $dbh->DataHash(); if ( ! $dbrow{ ' ' } == "" ) { $sqlstmt = "DELETE FROM table WHERE = '$ ';"; $dbh->Sql("$sqlstmt"); #create Web page $msg = $ . " has been deleted from our mailing list."; Print_Pg_Hdr(); print "$msg \n"; print " \n"; } else { $msg = "$ is not on our list."; Print_Pg_Hdr(); print "$msg \n"; print " \n"; } $dbh->Close(); } #end remove
sub Print_Pg_Hdr { print " \n"; print " CIS Newsletter Mailing List \n"; print " \n"; print " CIS Newsletter Mailing List \n"; } #end Print_Pg_Hdr