kjg has asked for the wisdom of the Perl Monks concerning the following question:
I've got a script producing a form using CGI:
This is the SQL statement I want it to execute:use strict; use diagnostics; use CGI qw(:standard); use CGI::Carp qw/fatalsToBrowser/; use Win32::ODBC; use lib '/perlcgi/settings'; require 'settings.pl'; # Included configuration file which contains g +lobal variables my %labels = ( MA => 'Mortgage Advisers', CA => 'Customer Advisers', BM => 'Branch Management', HO => 'Head Office', Acc => 'Accord', MSa => 'MCC Sales', MSe => 'MCC Service', ); $today = 'somedate' my $cgi = CGI->new; print $cgi->header('text/html'); my $dept = $cgi->param('department'); if (defined $dept) { if (exists $labels{$dept}) { # FIXME # untaint $dept and put it into database print $cgi->start_html, $cgi->p("$dept was received."), $cgi->end_html; } else { print $cgi->start_html, $cgi->p("$dept was received, but is not a valid department + name."), $cgi->end_html; }; } else { print $cgi->start_html, $cgi->start_form( -action => $cgi->script_name, ), $cgi->popup_menu( -name => 'department', -values => [keys %labels], -labels => \%labels, ), $cgi->submit, $cgi->end_form, $cgi->end_html; };
My problem is connecting to the database and then executing the SQL statement from within the CGI. I can't use DBI or anything wonderful like that, so I'm stuck with Win32::0DBC. I just can't figure out how to build this into the CGI way of laying stuff out. My usual way of connecting like this:# $SqlStatement = "SELECT * FROM Pipeline WHERE Publish<='$today' + AND Expiry>='$today' AND $dept='Must Read'";
just won't work. ($DSN is defined in settings.pl)if (!($db=new Win32::ODBC($DSN))) { print "Error connecting to Database\n"; print "Error: " . Win32::ODBC::Error() . "\n"; } $SqlStatement = "SELECT * FROM BulletinGroups"; if ($db->Sql($SqlStatement)) { print "SQL failed.\n"; print "Error: " . $db->Error() . "\n"; } else { while($db->FetchRow()) { Do something here }
I'm totally confused.
Any help would be gratefully appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: CGI executing a SQL statement
by Mr. Muskrat (Canon) on Apr 18, 2006 at 15:00 UTC | |
by kjg (Sexton) on Apr 18, 2006 at 15:11 UTC | |
by Mr. Muskrat (Canon) on Apr 18, 2006 at 15:33 UTC | |
by CountZero (Bishop) on Apr 18, 2006 at 19:58 UTC | |
by Anonymous Monk on Apr 18, 2006 at 15:47 UTC |