[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] Using DBI in CGI::Application


First, please go easy on me. I am firly new to PPerl and
CGI::Application. I have not found the answer to my question in
extensive Google and archive searching.

Basically I want to do the following in CGI::Application (this example
works on its own but I can't make it work in my CGI:Application module):

#! /usr/local/bin/Perl -w
use strict;
use DBI;

my $dbh;
my $sth;
my $sql;

$dbh =
DBI->connect('dbi:SQLite:/Library/Webserver/CGI-Executables/cgiDev/dbitest/exams.db')
or die $DBI::errstr;
$sql = "INSERT INTO profs (profName) VALUES (?)";
$sth = $dbh->prepare($sql);
$sth->execute('Prof BS');
$dbh->disconnect();

Works fine. 

Now in my Course Manager.pm I run into trouble file (look at the
add_instructor subroutine- also I know I want to have the db connection
shared amoung subs but I'm just trying to get this bugger to work first)
:

package Course_manager;
use base 'CGI::Application';
use DBI;
use strict;
use warnings;

sub setup{
    my $self = shift;
    $self->run_modes(
	'mode_1'=>'add_instructor',
	'mode_2'=>'add_course',
	'mode_3'=>'edit_instructor',
	'mode_4'=>'edit_exam',
	'mode_5'=>'update_course'
    );
    $self->start_mode('mode_1');
    $self->mode_param('rm');
    }

sub teardown{
    my $self = shift;
    #my $dbh = $self->param('dbh');
    #$dbh->disconnect();
}

sub add_instructor{
    my $self = shift;
    my $sql;
    my $dbh;
    my $sth;
    my $output;
    $dbh =
DBI->connect('dbi:SQLite:/Library/Webserver/CGI-Executables/cgiDev/dbitest/exams.db')
or die "Error: " . $DBI::errstr;
    $sql = "INSERT INTO profs (profName) VALUES ('Prof BBB')";
    #$sql = "SELECT * FROM profs";
    $sth = $dbh->prepare($sql);
    $sth->execute();
    $output = "text";
    return $output;
}

sub edit_instructor {
    my $self = shift;
    my $qry = $self->query(); #CGI.pm object
    my $sql;
    my $dbh = $self->param('dbh');
    my $output;
    $output = $qry->start_html(-title=>'Edit Instructor') . "\n";
    $sql = "SELECT * FROM profs";
    my $sth = $dbh->prepare($sql);
    $sth->execute();
    while (my $ref = $sth->fetchrow_hashref()){
	$output .= $ref->{'profName'} . "<br/>\n";
    }
    return $output;
}
sub update_course{
    my $self = shift;
    my $output = "Edit course  code to go here\n";
    return  $output;
}
sub add_course {
    my $self = shift;
    my $qry = $self->query(); #CGI.pm object 
    my $output  = $qry->start_html(-title=>'Add a Course to the Exam
Archive'). "\n";
    my @url = split(/\?/, $qry->self_url); #url of this page is $url[0]
    $output .= $qry->start_form(-action=>$url[0] . "?rm=mode_2",
-method=>'post') . "\n";
    $output .= $qry->h3('Add a Course to the exam Archive') . "\n";
    $output .= "Name of course: ";
    $output .= $qry->textfield(-name=>'courseName',
-size=>'50',-maxlength=>'75');
    $output .= $qry->submit(-name=>'Add Course');

    $output .= $qry->end_form();
    $output .= $qry->end_html();
    return $output;
}
sub edit_exam{
    my $self = shift;
    my $output = "This is the edit exam course form\n";
}

1;

Then here is my instance script:
#! /usr/local/bin/Perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Course_manager;

my $app = new Course_manager;
$app->run();


Apache error log returns this error:
courseApp.pl: Use of uninitialized value in subroutine entry at
Course_manager.pm line 37.
courseApp.pl: DBD::SQLite::st execute failed:  at Course_manager.pm line
37.

Suggestions are appreciated.
Thanks

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.