CGI::Application and SQLite - perl

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

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.

Related

Handle POST data sent as array

I have an html form which sends a hidden field and a radio button with the same name.
This allows people to submit the form without picking from the list (but records a zero answer).
When the user does select a radio button, the form posts BOTH the hidden value and the selected value.
I'd like to write a perl function to convert the POST data to a hash. The following works for standard text boxes etc.
#!/usr/bin/perl
use CGI qw(:standard);
sub GetForm{
%form;
foreach my $p (param()) {
$form{$p} = param($p);
}
return %form;
}
However when faced with two form inputs with the same name it just returns the first one (ie the hidden one)
I can see that the inputs are included in the POST header as an array but I don't know how to process them.
I'm working with legacy code so I can't change the form unfortunately!
Is there a way to do this?
I have an html form which sends a hidden field and a radio button with
the same name.
This allows people to submit the form without picking from the list
(but records a zero answer).
That's an odd approach. It would be easier to leave the hidden input out and treat the absence of the data as a zero answer.
However, if you want to stick to your approach, read the documentation for the CGI module.
Specifically, the documentation for param:
When calling param() If the parameter is multivalued (e.g. from multiple selections in a scrolling list), you can ask to receive an array. Otherwise the method will return the first value.
Thus:
$form{$p} = [ param($p) ];
However, you do seem to be reinventing the wheel. There is a built-in method to get a hash of all paramaters:
$form = $CGI->new->Vars
That said, the documentation also says:
CGI.pm is no longer considered good practice for developing web applications, including quick prototyping and small web scripts. There are far better, cleaner, quicker, easier, safer, more scalable, more extensible, more modern alternatives available at this point in time. These will be documented with CGI::Alternatives.
So you should migrate away from this anyway.
Replace
$form{$p} = param($p); # Value of first field named $p
with
$form{$p} = ( multi_param($p) )[-1]; # Value of last field named $p
or
$form{$p} = ( grep length, multi_param($p) )[-1]; # Value of last field named $p
# that has a non-blank value

PERL | Not getting the value after second submit

everyone.
I'm doing an tool with Perl that actually verifies a status in the database and if necessary it gets the properly queries and after confirmation it updates the value.
The page has three possible contents:
If the query that will be used is already filled it updates and returns the status.
If only the serial that the user input is filled it generates the query and then asks for confirmation.
If none of those 2 variables are already filled it goes to the main form where the user can submit the serial.
The problem that I'm facing is that after it generates the $final_query and shows up for confirmation, when I click the Confirm button it reloads the page but it pass directly through the if($final_query) and even the elseif that verifies if the $serial_no is already set.
Does Perl really lose those values if I perform a second submit or am I doing something wrong?
I'd love to have some explanation about it because it's the second time that I'm doing something with that language.
Thanks in advance!
--edit
I chopped the code to show where it defines the $final_query but I kept the structure to help in the understanding.
The full code is available at http://pastebin.com/6NqhbVau
#headers
if ($final_query) {
$content = "<h1>first if</h1>";
#updateESNDatabase($database, $final_query);
#it only enters here if the user type the ESN
}elsif ($serial_no) {
#selects the database
switch(checkUpdateNeeded($database, $serial_no)) {
case 0 {
#Shows that the updates are no needed
}
case 1 {
$final_query = `cat $query1`;
chop($final_query);
$final_query =~ s/SERIALNUM/$serial_no/g;
$final_query =~ s/LOGINID/$login_id/g;
$content = $cgi->start_form .
"<center>" .
"<h3> Please double check the queries below before you update on database </h3>" .
"</center>" .
$cgi->submit("Confirm") . $cgi->end_form;
$content .= $final_query;
}
case 2 {
#Makes almost the same as the first case, it only uses a different file to generate the query.
}
}
} else {
#Generates the first page, where the users inputs information
}
$page->set_content($content);
$page->process;
I figured out how to perform this.
As per my analysis I cannot simply create a variable and pass it through the cgi form.
To workaround this what I've done is create a hidden input in HTML and then send the variable through it.
<input type='hidden' name='the_query' value=\"$final_query\">

Why would value passed through $m->comp() lose its value irregularly?

I have a perl mason component which is called to display an html page containing threaded comments. It uses Class DBI for loading from a MySQL database.
The problem is that sometimes, and I mean sometimes, very irregularly a variable loses its value partway through the code. I am not changing the code between when it starts and stops happening, just reloading the page. It's not even that it just has the value on one page load and not the next. It's that on one page load you can print something to show that the variable contains a ref to an object (a "Person" with a name, etc.) and later on in the code on that same page load you can print that again and show that it doesn't. On the next page load the variable may retain its value all the way through. The only thing that is happening is that the variable gets passed through a call to $m->comp(), and a default gets applied if it is empty.
Further, it happens for each and every comment, effectively losing its value many times in the same page load.
Unfortunately (or fortunately, depending on how you look at it) I can't post all the code involved verbatim, but it boils down to the following, note the two commented lines marked "HERE":
<%init>
my #comments = $dc->document->search("type = 'comment'");
</%init>
<div>
<& '.comments', all_comments => \#comments &>
</div>
<%def .comments>
<%args>
$all_comments
</%args>
<%init>
my #comments;
#comments = #$all_comments;
</%init>
% for my $c (#comments) {
% my $poster = p($c->get_value('poster'));
% $poster = Person->get_anonymous unless ref $poster;
% # HERE: The variable is a ref to a given Person.
% $m->comp('.comment', poster => $poster);
% }
</%def>
<%def .comment>
<%args>
$poster => Person->get_anonymous
</%args>
<%init>
# HERE: The variable is now the result of Person->get_anonymous instead.
unless (ref $poster) {
$poster = Person->get_anonymous;
}
</%init>
<p><% $poster->id == 1 ? ' (anonymous)' : $poster->fullname %></p>
</%def>
I've tried removing that default, in which case I get an empty variable.
This is a years old problem scrutinized down to these few lines of code where, programmatically speaking, something impossible seems to be happening. I'm left with a possible bug in Mason, or maybe some combination of things like some voodoo between Class DBI and the database losing a connection, or something I don't know about $m->comp().
The only other clue I have is that when I've added something to the page, I've had the problem show up to an entirely new variable. Unfortunately, I never know when the problem is going to happen, and it happens seldom enough that I won't be able to go throw some suggested debugging code into it that will immediately give me something to report back. I'm only hoping for the off chance that somebody has experienced something similar or knows some possible problem that might explain what is going on.

Manipulating form input values after submission causes multiple instances

I'm building a form with Yii that updates two models at once.
The form takes the inputs for each model as $modelA and $modelB and then handles them separately as described here http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/
This is all good. The difference I have to the example is that $modelA (documents) has to be saved and its ID retrieved and then $modelB has to be saved including the ID from $model A as they are related.
There's an additional twist that $modelB has a file which needs to be saved.
My action code is as follows:
if(isset($_POST['Documents'], $_POST['DocumentVersions']))
{
$modelA->attributes=$_POST['Documents'];
$modelB->attributes=$_POST['DocumentVersions'];
$valid=$modelA->validate();
$valid=$modelB->validate() && $valid;
if($valid)
{
$modelA->save(false); // don't validate as we validated above.
$newdoc = $modelA->primaryKey; // get the ID of the document just created
$modelB->document_id = $newdoc; // set the Document_id of the DocumentVersions to be $newdoc
// todo: set the filename to some long hash
$modelB->file=CUploadedFile::getInstance($modelB,'file');
// finish set filename
$modelB->save(false);
if($modelB->save()) {
$modelB->file->saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$modelB->file);
}
$this->redirect(array('projects/myprojects','id'=>$_POST['project_id']));
}
}
ELSE {
$this->render('create',array(
'modelA'=>$modelA,
'modelB'=>$modelB,
'parent'=>$id,
'userid'=>$userid,
'categories'=>$categoriesList
));
}
You can see that I push the new values for 'file' and 'document_id' into $modelB. What this all works no problem, but... each time I push one of these values into $modelB I seem to get an new instance of $modelA. So the net result, I get 3 new documents, and 1 new version. The new version is all linked up correctly, but the other two documents are just straight duplicates.
I've tested removing the $modelB update steps, and sure enough, for each one removed a copy of $modelA is removed (or at least the resulting database entry).
I've no idea how to prevent this.
UPDATE....
As I put in a comment below, further testing shows the number of instances of $modelA depends on how many times the form has been submitted. Even if other pages/views are accessed in the meantime, if the form is resubmitted within a short period of time, each time I get an extra entry in the database. If this was due to some form of persistence, then I'd expect to get an extra copy of the PREVIOUS model, not multiples of the current one. So I suspect something in the way its saving, like there is some counter that's incrementing, but I've no idea where to look for this, or how to zero it each time.
Some help would be much appreciated.
thanks
JMB
OK, I had Ajax validation set to true. This was calling the create action and inserting entries. I don't fully get this, or how I could use ajax validation if I really wanted to without this effect, but... at least the two model insert with relationship works.
Thanks for the comments.
cheers
JMB

After querying DB I can't print data as well as text anymore to browser

I'm in a web scripting class, and honestly and unfortunately, it has come second to my networking and design and analysis classes. Because of this I find I encounter problems that may be mundane but can't find the solution to it easily.
I am writing a CGI form that is supposed to work with a MySQL DB. I can insert and delete into the DB just fine. My problem comes when querying the DB.
My code compiles fine and I don't get errors when trying to "display" the info in the DB through the browser but the data and text doesn't in fact display. The code in question is here:
print br, 'test';
my $dbh = DBI->connect("DBI:mysql:austinc4", "*******", "*******", {RaiseError => 1} );
my $usersstatement = "select * from users";
my $projstatment = "select * from projects";
# Get the handle
my $userinfo = $dbh->query($usersstatement);
my $projinfo = $dbh->query($projstatement);
# Fetch rows
while (#userrow = $userinfo->fetchrow()) {
print $userrow[0], br;
}
print 'end';
This code is in an if statement that is surrounded by the print header, start_html, form, /form, end_html. I was just trying to debug and find out what was happening and printed the statements test and end. It prints out test but doesn't print out end. It also doesn't print out the data in my DB, which happens to come before I print out end.
What I believe I am doing is:
Connecting to my DB
Forming a string the contains the command/request to the DB
Getting a handle for my query I perform on the DB
Fetching a row from my handle
Printing the first field in the row I fetched from my table
But I don't see why my data wouldn't print out as well as the end text. I looked in DB and it does in fact contain data in the DB and the table that I am trying to get data from.
This one has got me stumped, so I appreciate any help. Thanks again. =)
Solution:
I was using a that wasn't supported by the modules I was including. This leads me to another question. How can I detect errors like this? My program does in fact compile correctly and the webpage doesn't "break". Aside from me double checking that all the methods I do use are valid, do I just see something like text not being displayed and assume that an error like this occurred?
Upon reading the comments, the reason your program is broken is because query() does not execute an SQL query. Therefore you are probably calling an undefined subroutine unless this is a wrapper you have defined elsewhere.
Here is my original posting of helpful hints, which still apply:
I hope you have use CGI, use DBI, etc... and use CGI::Carp and use strict;
Look in /var/log/apache2/access.log or error.log for the bugs
Realize that the first thing a CGI script prints MUST be a valid header or the web server and browser become unhappy and often nothing else displays.
Because of #3 print the header first BEFORE you do anything, especially before you connect to the database where the script may die or print something else because otherwise the errors or other messages will be emitted before the header.
If you still don't see an error go back to #2.
CGIs that use CGI.pm can be run from a command line in a terminal session without going through the webserver. This is also a good way to debug.