in reply to querying a database using checkbox values

OK here goes...
A. if ($initiative = 'on') assigns variable and returns true it does not evaluate try  if ($initiative eq 'on')
B. What are the checkboxes returning? would s/\W//g be more effective stripping non word charecters?
C. If you are using checkboxes rather than radio boxes then multiple ones can be checked.
my $hash ={ Initiative=>'DriveInitiative', ResOrientation => 'DriveResOrient'}; my @clauses = (); foreach my $checkbox (keys %$hash) { push @clauses, $hash->{$checkbox} if validate($cgi->param($checkbox +)); } my $where_clause = join(' and ',map($_.'= 'on', @clauses)); my $sql = "select a.ResType, a.ResLevel, a.ResManage a.Details, a.Length, a.Source, a.Cost, a.FurtherDeta +ils from Resources a INNER JOIN ResourceSettings b ON a.ResID = b.ResID"; $sql .= " where $where_clause " if ($where_clause); $sql .= " order by a.ResType, a.ResLevel, a.ResManage, a.Details, a.Length, a.Source, a.Cost, a.FurtherDetail +s";

Code is untested:(
Hope this helps
UnderMine

Replies are listed 'Best First'.
Re: Re: querying a database using checkbox values
by Rachel (Acolyte) on Nov 19, 2002 at 11:38 UTC
    Thanks, I tried that and got this error Bad name after on'. This error was on line 34 which is this line;
    my $where_clause = join(' and ',map($_.'= 'on', @clauses));
    I thought this was due to the quotes not being closed at first but then I closed them and still got the same error so it seems like it doesn't like @clauses. Thanks to everyone who replied to my question.
      Ok, I had an extra quote and now this has been sorted, I am back to my blank screen. Don't you just love life!?;)
        This is my ammended code, if anyone has any more ideas for stuff I could try, because it is still not giving me any output OR error messages........... This would be much appreciated.
        #!c:/perl/bin/perl.exe -w 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'); my $hash ={ Initiative=>'DriveInitiative', ResOrientation => 'DriveResOrient', Creativity => 'DriveCreativity', ChangeOrientation => 'DriveChangeOrient', DecisionMaking => 'DriveDecisionMake', SelectLevel => 'ResLevel', ManagesPeople => 'ResManage'}; my @clauses = (); foreach my $checkbox (keys %$hash) { push @clauses, $hash->{$checkbox} if validate($cgi->param($chec +kbox)); } my $where_clause = join(' and ',map($_.= 'on', @clauses)); 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 $where_clause " if ($where_clause); $sql .= " order by a.ResType, a.ResLevel, a.ResManage, a.Details, a.Length, a.Source, a.Cost, a.FurtherDetail +s"; my $sth = $dbh->prepare($sql); $sth->execute || die "Could not execute SQL statement ... " . $dbh->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 perc +ents $string =~ s/[^A-Za-z0-9%]//g; return $string; }