Contributed by scratch
on Aug 22, 2000 at 03:02 UTC
Q&A
> CGI programming
Description: I'm using CGI.pm to collect some info. Right now I'm just
writing the values out to a file with the save_parameters
function, but I'd like to write the data to a database.
I think I can do this with the DBI module - and I'll look
into that more - but I've got sort of a general question:
How can I take a checkbox_group variable or question from
the form and write its values (stored in an array in
perl) into a database? Do I somehow need to pull out each
of the array elements into scalar vars before I try writing
data out to a database? I'm getting sort of stumped
thinking about how to do this.
Thanks a lot for any suggestions!
Answer: How do I use a database and handle checkboxes? contributed by Ovid There are many ways of addressing this issue. The following is not the best, but it works. I have a form where teachers can enter lesson plans and a series of check boxes represent the appropriate grade levels, K through 12. The checkboxes are named GradeK, Grade1, ... Grade12.
When this data is sent via CGI, the parameter is sent to the script as GradeK=on (or whatever value you set the checkbox at). No checkbox parameter is sent if it's not checked. Therefore, I use the following sub to pull the data:
sub getGrades {
my $grades;
my @keys = $template->param;
# Look for the grade parameters and add each one to $grades
foreach (@keys) {
$grades .= $1 . ',' if /^Grade([1-9]|1[0-2]|K)$/;
}
$grades =~ s/,$//; # remove trailing comma
return $grades;
}
This sub is called from the following sub which uses DBI to write the data to a MySQL database:
sub storeData {
my $table = shift;
my $grades = getGrades();
my $standards = getStandards();
use DBI;
my $dbh = DBI->connect("DBI:mysql:$database",$user,$password);
$dbh->{RaiseError} = 1;
# save template information to database
my $sql = 'INSERT INTO ' . $table . '(title, lesson_summary,
grades, standards)
VALUES (?, ?, ?, ?)';
my $sth = $dbh->prepare($sql);
$sth->execute(
$template->param('Title'),
$template->param('Summary'),
$grades,
$standards
);
$template->end_html;
$sth->finish;
$dbh->disconnect;
print $template->redirect('http://www.someserver.com/some/path/tha
+nks.html');
}
This should give you a good start on how to approach this (Grades is defined as varchar(28), by the way).
Cheers,
Ovid | Answer: How do I use a database and handle checkboxes? contributed by princepawn If you use DBIx::Recordset which is built on top of DBI, you can commit form data to database in one line of DBIx::Recordset:
@names = $q->param();
map { $fdat{$_}=$q->param($_} } @names;
DBIx::Recordset->Insert({%connect_dsn,%fdat});
For more information on DBIx::Recordset, visit:
http://perl.apache.org/embperl/index.html |
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|