dmux
dmux

Reputation: 442

CGI::Application and SQLite

I've been messing around with CGI::application the past couple of days and decided to create a really basic forum: the first page displays all posts (only first level, no replies or anything) and a form which can be used to create a new post.

The issue I'm running into is that the data that gets entered into the form never gets inserted into the SQLite database.

Here's the sub procedure I'm having trouble with:

sub newpost {

my $self = shift;


if ( $self->param() ){

    my $dbh = DBI->connect("dbi:SQLite:dbname=$database_file","","");



    my $sth = $dbh->prepare("INSERT INTO posts (author, time, text)        VALUES('testuser', '2011-10-23', 'This is a test!')");


    $sth->execute();

    $self->header_type('redirect');
    $self->header_props(-url=> '?rm=viewall');

}

else {

    my $tmpl_obj = $self->load_tmpl('newpost.html');

    return $tmpl_obj->output();

}

What happens correctly is that when the newpost run mode is first called, the code within the else statement is run (the template with the form is loaded). The action for the form calls this same run mode, but now that parameters are being provided, the code in the if statement is run. I've checked the SQL code itself and it works, so there must be something else I'm over looking.

Also, is it considered best practice to go about implementing the form logic in this way?

Thanks

Upvotes: 1

Views: 220

Answers (1)

mpeters
mpeters

Reputation: 4778

You're confusing $self->param() with $self->query->param. The 1st is per-request application level parameters (stuff you might set in one method and use again in another method) and the 2nd are the parameters from the GET query string or the POST body of the request. If you're expecting something from the user it will be in $self->query->param.

BTW, the $self->query object is a normal CGI object, so see it's documentation for specifics.

Upvotes: 2

Related Questions