List:MySQL and Perl« Previous MessageNext Message »
From:mark o' sullivan Date:October 31 2003 4:23pm
Subject:Session database error
View as plain text  
Hi,
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 .= "<option value=\"$my_
approved_option\"";
               $my_approved_detail .= "selected" if ( $my_approved_option
eq $approved );
               $my_approved_detail .= ">$my_approved_option</option>";
        }

print "<html><head><title>Task to be added for user to waiting room
table</title>";
print "</head>";
print "<body bgcolor=\"e6e6f7\">";
print "<font face=\"arial\" size=\"2\"><b><tr valign=\"top\" align =
\"middle\"><i>Hi. Add a new task to your user profile
:<p></i></td> </tr>";
print "<form action=\"addTask.cgi\" method=\"post\">  <input
type=\"hidden\" name=\"submit\"
value=\"Submit\"><p>$error_message</p><table>";
print "<tr><td><b>Task:</b> </td><td><input
type=\"text\" maxLength =
\"100\" name=\"task\" value=\"$your_task\"></td></tr>";
print "<tr><td><b>Estimated Size:</b>
</td><td><input type=\"text\"
maxLength = \"3\" size=\"3\" value=\"$your_weeks\" name=\"weeks\"> <b>weeks
</b> <input type=\"text\" maxLength = \"3\" size=\"3\" name=\"days\"
value=\"$your_days\"> <b> days </b></td></tr>";
print "<tr><td><b>Approved:</b></td><td><SELECT
NAME =approved value =
\"approved\">$my_approved_detail</SELECT></td></tr>";
print "<tr><td><b>Completion
Date:</b></td><td><SELECT NAME=formMonth
value=\"$formMonth\"><OPTION VALUE=Jan>Jan<OPTION VALUE=Feb>Feb<OPTION
VALUE=Mar>Mar<OPTION VALUE=Apr>Apr<OPTION VALUE=May>May<OPTION
VALUE=Jun>Jun<OPTION VALUE=Jul>Jul<OPTION VALUE=Aug>Aug<OPTION
VALUE=Sep>Sep<OPTION VALUE=Oct>Oct<OPTION VALUE=Nov>Nov<OPTION
VALUE=Dec>Dec</SELECT>-";
print "<SELECT NAME=formDay  value = \"$formDay\"><OPTION VALUE=1>1<OPTION
VALUE=2>2<OPTION VALUE=3>3<OPTION VALUE=4>4<OPTION
VALUE=5>5<OPTION
VALUE=6>6<OPTION VALUE=7>7<OPTION VALUE=8>8<OPTION
VALUE=9>9<OPTION
VALUE=10>10<OPTION VALUE=11>11<OPTION VALUE=12>12<OPTION
VALUE=13>13<OPTION
VALUE=14>14<OPTION VALUE=15>15<OPTION VALUE=16>16<OPTION
VALUE=17>17<OPTION
VALUE=18>18<OPTION VALUE=19>19<OPTION VALUE=20>20<OPTION
VALUE=21>21<OPTION
VALUE=22>22<OPTION VALUE=23>23<OPTION VALUE=24>24<OPTION
VALUE=25>25<OPTION
VALUE=26>26<OPTION VALUE=27>27<OPTION VALUE=28>28<OPTION
VALUE=29>29<OPTION
VALUE=30>30<OPTION VALUE=31>31</SELECT>-";
print "<SELECT NAME=formYear value = \"$formYear\"><OPTION
VALUE=2003>2003<OPTION VALUE=2004>2004<OPTION VALUE=2005>2005<OPTION
VALUE=2006>2006<OPTION VALUE=2007>2007</SELECT>";
print "</td></tr>";

print "<tr><td><b>Success Measured By:</b>
</td><td><input type=\"text\"
maxLength = \"100\" name=\"successMeasuredBy\"
value=\"$successMeasuredBy\"></td></tr>";
print "</table><br>";
print "<input type=\"submit\" name=\"submit\" value=\"Submit\">";
print "</form>";
print "</body></html>";
}

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<br>" 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=localhost',
                                             $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 "<html><head><title>Thank
You</title></head>";
         print "<body>Thank you - your data has been entered correctly into
database!</body></html>";

    }
}

# 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
**********************************************************************

Thread
Session database errormark o' sullivan31 Oct
  • Re: Session database errorTony Dean3 Nov