Session database error
am 31.10.2003 16:23:02 von mark.r.osullivanHi,
Anyone know how I can retain session information when I repaint a form in
perl which has invalid textfields and then link to a database. The way my
code works is as follows:
Subroutines:
validate_form -> valids input from perl form, if incorrect repaints form
with error message.
display_form-> displays the current form
process_form-> everything okay and link to database.
The problem here is retaining the $user and $password to link to database
if error occurs. Any idea how to get around this.
#!E:\Perl\bin\perl.exe
use DBI;
use DBD::mysql;
use vars qw($ob,$user,$password);
use CGI qw(:cgi); # Include CGI functions
use CGI::Carp qw(fatalsToBrowser);
use Session;
$query = new CGI;
print $query->header;
$ob = Session->new();
$ob->open_session(0);
# extract name and password from session
$user = $ob->{SESSION}{user};
$password = $ob->{SESSION}{password};
print "The user is $user";
print "The password is $password";
sub display_form
{
my $error_message = shift;
my $your_task = shift;
my $your_weeks = shift;
my $your_days = shift;
my $approved = shift;
my $formMonth = shift;
my $formDay = shift;
my $formYear = shift;
my $successMeasuredBy = shift;
print "The error message is $error_message";
# Build "selected" HTML for the "Approved" drop-down list
my $my_approved_detail = "";
my @my_approved_opts = ("IVM-1136","WAG");
foreach my $my_approved_option ( @my_approved_opts )
{
$my_approved_detail .= "
approved_option\"";
$my_approved_detail .= "selected" if ( $my_approved_option
eq $approved );
$my_approved_detail .= ">$my_approved_option";
}
print "
table
print "";
print "";
print "
";
print "
print "";
}
sub validate_form
{
#general variables associated with form
my $your_task = $query->param("task");
my $your_weeks = $query->param("weeks");
my $your_days = $query->param("days");
my $approved = $query->param("approved");
my $formMonth = $query->param("formMonth");
my $formDay = $query->param("formDay");
my $formYear = $query->param("formYear");
my $successMeasuredBy = $query->param("successMeasuredBy");
my $error_message = "";
$error_message .= "Please enter your task
" if ( !$your_task);
if ( $error_message )
{
# Errors with the form - redisplay it and return failure
display_form
($error_message,$your_task,$your_weeks,$your_days,$approved, $formMonth,$form
Day,$formYear,$successMeasuredBy);
return 0;
}
else
{
return 1;
}
}
sub process_form
{
if ( validate_form ( ) )
{
# connect to database
my $dbh =
DBI->connect('DBI:mysql:database=waitingroomdetail;host=loca lhost',
$user, $password)
or die $DBI::errstr;
# prepare and execute query
$query = "INSERT INTO waitingroom (candidate, task,
estimatedSize, approved, completionDate, successMeasuredBy)
VALUES ('$user', '$task', '$estimatedSize',
'$approved','$completionDate','$successMeasuredBy')";
$sth = $dbh->prepare($query);
$sth->execute();
print "$user $password Record inserted into the database";
$sth->finish();
# disconnect from database
$dbh->disconnect;
print "
print "Thank you - your data has been entered correctly into
database!";
}
}
# Process form if submitted; otherwise display it
if ( $query->param("submit") )
{
process_form ( );
}
else
{
print "The user is $user";
print "The password is $password";
display_form ( );
}
$ob->close_session();
************************************************************ **********
This document is strictly confidential and is intended for use by
the addressee unless otherwise indicated.
Allied Irish Banks
************************************************************ **********
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org