DBM Databases Please use speaker notes for additional information!
Click here.
Make any changes you want. Click here.
Click here to retrieve the information.
Click here.
Click here
Click here.
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 #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, $ ); #assign values to variables $button = param('button'); $name = param('stuname'); $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***** The functions add(), remove() and view() are shown on the next few pages.
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{$ }); if (exists($mail{$ })) { untie(%mail); #create Web page print " \n"; print " CIS Newsletter Mailing List \n"; print " \n"; print "Student Name: \n"; print "Student Major: \n"; print "Student Option: \n"; print " address: \n"; print " \n"; print " OR...Click here to return to main page \n"; print " \n"; } else { untie(%mail); #create Web page print " \n"; print " CIS Newsletter Mailing List \n"; print "Record does not exist. \n"; print " Click here to return to main page \n"; print " \n"; } } #endview This is the code that opens the database. Note that I am opening in as readonly. I then take the record that the key located and split it into the appropriate fields so I can display them on the screen.
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{$ } = "$name,$major,$option"; untie(%mail); #create Web page print " \n"; print " CIS Newsletter Mailing List \n"; print "You are on our list to receive your newsletter at $ . \n"; print " Click here to return to main page \n"; print " \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{$ })) { delete($mail{$ }); $msg = "Mail will not be sent to $ "; } else { $msg = "Our mailing list does not include $ ."; } #close database untie(%mail); #create Web page print " \n"; print " CIS Newsletter Mailing List \n"; print "$msg \n"; print " Click here to return to main page \n"; print " \n"; } #end remove
#!/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, $ ); #assign values to variables $button = param('button'); $name = param('stuname'); $major = param('major'); $option = param('option'); $ = param(' '); 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{$ } = "$name,$major,$option"; untie(%mail); #create Web page print " \n"; print " CIS Newsletter Mailing List \n"; print "The information has been changed. \n"; print "Name: $name \n"; print "Major: $major \n"; print "Option: $option \n"; print " $ \n"; print " Click here to return to main page \n"; print " \n"; } #end change