#!C:\Perl\bin\perl.exe -w use CGI ':standard'; use DBI; print header; print start_html('Hometown Bank'), h1({align=>'center'},'Hometown Bank'), start_form,p, "Enter userID: ",textfield('name'), " Enter password: ",password_field('password'), p; print end_html; # if parameters have already been supplied then expand form to include transactions if (param()) { $action = param('Action'); $name=param('name'); $password=param('password'); $amount=param('amount'); &checkbalance; if ($balance eq "" ){ print "Sorry, the database was unable to verify your userID and password. Please try again later.",p; print submit('Check Balance')," "; print defaults('Cancel'); print end_form; print hr; die; } else { #print " UserID and password are valid!",p; } #--------------------------------------------------------------------------- if ($action eq "Deposit" ) { $balance=$balance+$amount; print h3("\$$amount Deposited"),p; } if ($action eq "Withdraw") { $balance=$balance - $amount; if ($balance>=0){ print h3("\$$amount Withdrawn"),p; } if ($balance<0){ print h3("Insufficient Funds!"),p; print submit('Check Balance')," "; print defaults('Cancel'); print end_form; #print hr; die; } } &update; #--------------------------------------------------------------------------- print h3("Balance: \$$balance"),p, "Enter Amount: ",textfield('amount',' '),p, submit('Action','Deposit')," ", submit('Action','Withdraw')," ", defaults('Cancel')," "; } # end "long form" #--------------------- # if parameters haven't been supplied then show intro form else { #print " param() is 0\n"; print submit('Check Balance'), " ", defaults('Cancel'); } print end_form; print end_html; #--------------------------------------------------------------------------------- sub update { $string="update hometown set balance=".$balance." where username=\"".$name."\" and password=\"".$password."\" "; $dbh = DBI->connect("DBI:mysql:bank", ,,{RaiseError => 1, AutoCommit =>0}); $sth = $dbh->prepare($string); $sth->execute; #print "\$sth after execute= $sth",p; $sth->finish; $dbh->disconnect; } #end subroutine update #---------------------------------------------------------------------------------- sub checkbalance { $string="select balance from hometown where username=\"".$name."\" and password=\"".$password."\" "; $dbh = DBI->connect("DBI:mysql:bank", ,,{RaiseError => 1, AutoCommit =>0}); $sth = $dbh->prepare($string); $sth->execute; @row=$sth->fetchrow_array; $balance=$row[0]; $sth->finish; $dbh->disconnect; } # end subroutine checkbalance