Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl/cgi and an Access Database Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Perl/cgi and an Access Database Please use speaker notes for additional information!"— Presentation transcript:

1 Perl/cgi and an Access Database Please use speaker notes for additional information!

2

3

4

5 BCC Students BCC Student eMail 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: eMail address:

6 #!/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;

7 #*****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' } \n"; print "Major: $dbrow{ 'major' } \n"; print "Option: $dbrow{ 'option' } \n"; print "eMail: $dbrow{ 'email' } \n"; print " \n"; } else { $msg = "$email 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.

8

9 $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' } \n”; print "Major: $dbrow{ 'major' } \n"; print "Option: $dbrow{ 'option' } \n"; print "eMail: $dbrow{ 'email' } \n"; print " \n"; } else { $msg = "$email is not on our list. "; Print_Pg_Hdr(); print "$msg \n"; print " \n"; } $dbh->Close(); } #endview

10 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 \n"; print " \n"; } else { #create Web page Print_Pg_Hdr(); print "You are on our list to receive your newsletter at $email. \n"; print " \n"; } $dbh->Close(); } #end add Note the punctuation when inserting SQL into Perl.

11 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 \n"; print " \n"; } else { $msg = "$email is not on our list."; Print_Pg_Hdr(); print "$msg \n"; print " \n"; } $dbh->Close(); } #end remove

12 sub Print_Pg_Hdr { print " \n"; print " CIS Newsletter Mailing List \n"; print " \n"; print " CIS Newsletter Mailing List \n"; } #end Print_Pg_Hdr


Download ppt "Perl/cgi and an Access Database Please use speaker notes for additional information!"

Similar presentations


Ads by Google