Hi,

I hope someone can help me with this. I am new to Perl and I am trying to use form values "checkboxes", to query an access database.

I am having trouble getting the sql statement to use the form values in the query. It passes the values from the form to the perl script but then throws up a blank screen, so I have no error messages to work with either.

This is the script I am "trying" to use to parse the form values and query the database (without much success I might add!

20021119 Edit by Corion : Removed code tags from text

#!c:/perl/bin/perl.exe use strict; use DBI; use CGI; #open connection to Access database my $dbh = DBI->connect("dbi:ODBC:directory", { 'AutoCommit' =>1, 'RaiseError' =>1}) || die "Error connecting: '$DBI::errstr'"; # setup CGI handle my $cgi = new CGI; # start HTML print $cgi->header . $cgi->start_html('Drive'); # handle any queries that have been sent our way my $initiative = validate($cgi->param('Initiative')); my $resorientation = validate($cgi->param('ResOrientation')); my $creativity = validate($cgi->param('Creativity')); my $changeorientation = validate($cgi->param('ChangeOrientation')); my $decisionmaking = validate($cgi->param('DecisionMaking')); my $level = validate($cgi->param('SelectLevel')); my $manage = validate($cgi->param('ManagesPeople')); if ($cgi->param('Query')) { my $sql = "select a.ResType, a.ResLevel, a.ResManage a.Details, a.Length, a.Source, a.Cost, a.FurtherDetails from Resources a INNER JOIN ResourceSettings b ON a.ResID = b.ResID"; $sql .= "where DriveInitiative = $initiative" if ($initiative = 'on'); $sql .= "where DriveResOrient = $resorientation" if ($resorientation = 'on'); $sql .= "where DriveCreativity = $creativity" if ($creativity = 'on'); $sql .= "where DriveChangeOrient = $changeorientation" if ($changeorientation = 'on'); $sql .= "where DriveDecisionMake = $decisionmaking" if ($decisionmaking = 'on'); $sql .= "where ResLevel like $level"; $sql .= "where ResManage like $manage"; $sql .= "order by a.ResType, a.ResLevel, a.ResManage, a.Details, a.Length, a.Source, a.Cost, a.FurtherDetails"; my $sth = $dbh->prepare($sql); $sth->execute || die "Could not execute SQL statement ... maybe invalid?"; die "Error connecting: '$DBI::errstr'"; my $rows = $dbh->selectall_arrayref($sql) || die $dbh->errstr; if (@$rows) { print "<table border=1 cellspacing=0 cellpadding=3><tr>" . "<th>Type</th><th>Level</th><th>Manage</th><th>Details</th><th>Length< +/th><th>Source</th><th>Cost</th><th>Further Details</th></tr>"; foreach my $row (@$rows) { print "<tr><td>" . join ("</td><td>", @$row) . "</td></tr>\n"; } print "</table>\n"; } else { print "<p><i>No matches found</i></p>\n"; } } # disconnect from database $dbh->disconnect(); exit(0); # validate user input sub validate { my $string = shift; # get rid of all non-letter, non-numerical characters and percents $string =~ s/[^A-Za-z0-9%]//g; return $string; }

In reply to querying a database using checkbox values by Rachel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.