Are your searchable fields just matching for exact values, or are you looking for ranges, contains, etc. In any case, here is a simple way to do it if you're only looking for exact matches for each field.
...
my $form = CGI->new();
my $db_handle = DBI->connect(...);
my @fields = qw(f_1 f_2 f_3 f_4 f_5);
my @query_parts;
foreach my $field (@fields) {
if ( defined $form->param($field) ) {
push @query_parts, "$field = " . $db_handle->quote($form->para
+m($field));
}
}
if ( ! scalar(@query_parts) ) {
# do something else
return;
}
my $query = "SELECT * FROM Table_Name WHERE ";
$query .= join " AND ", map { "( $_ )" } @query_parts;
I use schemes like this, with a lot of additions in a number of production systems. This specific code is untested, but the idea is solid.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
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.