Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

DBI script runs from command line, not from CGI

by dru145 (Friar)
on Apr 16, 2002 at 15:36 UTC ( [id://159530]=perlquestion: print w/replies, xml ) Need Help??

dru145 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I can get the following code to run fine from command line (without the CGI stuff), but not in this CGI script. I am not getting any errors either (to the screen or in the logs). Anyone know what I'm doing wrong?

Snippet of code from the form which asks for the filename:
start_multipart_form( { -action => "action_upload.cgi", -enctype => "application/x-www-form-urlencoded", -method => "post" } ), .............some html table code................................. Tr( { -style => "background-color:#CCCCCC" }, td( strong( "Enter the file you want to upload" ) ), td( filefield( { -name=>'file', -default=>'starting value', -size=>30, -maxlength=>80 } ) ), #end td ), #end Tr
action_upload.cgi script:
#!/usr/bin/perl -w use strict; use DBI; use CGI::Carp 'fatalsToBrowser'; use CGI qw/:standard/; my $infile = upload ('file') or die "$!"; my $message1 = "<u>The data was inserted into the database successfull +y </u>"; my ($ip,$port); my %data; do_work(); dbi(); display_page("$message1"); sub do_work { while (<$infile>){ if (/(\d+\.\d+\.\d+\.\d+)\s+(\S+)/) { $ip = $1; $data{$ip}{cname} = $2; next; } if(m/^\s{1}\|___\s+(\d+)\s\s(.*)/){ $port = $1; $data{$ip}{ports}{$port}{service} = $2; $data{$ip}{ports}{$port}{banner} = ''; next; } if(m/^\t\s{1}\|___\s+(.*)/){ $data{$ip}{ports}{$port}{banner} = $1; next; } } } sub dbi { my ($i,$j); ### Enable error checking my %attr = ( PrintError => 0, RaiseError => 1, ); ### Connect to database my $dbh = DBI->connect("DBI:mysql:audit", username, password, \%attr) +; my $sth = $dbh->prepare("insert into Supersearch (ip, svr_name, port, + service, banner) values (?,?,?,?,?)"); for $i (keys %data) { for $j (keys %{$data{$i}{ports}}) { $sth->execute($i, $data{$i}{cname}, $j, $data{$i}{ports}{$j}{s +ervice}, $data{$i}{ports}{$j}{banner}); } } ### Disconnect from db $dbh->disconnect; } sub display_page { my $message = "$_[0]"; print header, start_html( "-title" => "Results Page"), p("&nbsp;"), p( strong( $message ) ), p("&nbsp;"), end_html; }
Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: DBI script runs from command line, not from CGI
by tachyon (Chancellor) on Apr 16, 2002 at 15:50 UTC

    As usual when a script runs from the command line but not under CGI you have to ask yourself what is different between these two scenarios? A: Permissions. On the command line the script runs as user:you, under CGI it runs with user:nobody and different permissions.

    When you say it runs from the command line but not under CGI what do you mean. Browser hangs, no output, wrong output? I have found that CGI::Carp 'fatalsToBrowser' does not catch all errors. As a result I use this in debugging:

    BEGIN { $|++; # autoflush buffers; use CGI::Carp 'fatalsToBrowser'; print "Content-type: text/html\n\n"; }

    This relaibly gets the errors to the browser and has the added bonus of printing whatever headers your script is sending in plaintext at the top of the window.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      tachyon,

      Thanks for the suggestion. I added those three lines to the top of my script, and still didn't get any errors. I should have said it does not run correctly under CGI. It runs, but it is not adding the data to the database. I'm just getting the display page that says the data was uploaded successfully, but it wasn't.

      I checked my permissions and they are the same as my other CGI scripts that work fine.

      Anything else I should check?

      Thanks,
      Dru
      Another satisfied monk.
        -enctype => "application/x-www-form-urlencoded"

        For file uploads to work the ENCTYPE must be "multipart/form-data". As you use start_multipart_form you don't need to specify an ENCTYPE as CGI.pm will specify it for you.

        You do no checking to see that you got your file uploaded (it does not) and that you got wrote data in %data (no file, no %data) so you have nothing to insert.

        A bit more error checking to ensure that the file arrived and that data was retieved might be in order, even though fixing the enctype should fix the problem.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: DBI script runs from command line, not from CGI
by wardk (Deacon) on Apr 16, 2002 at 16:25 UTC
    I don't see any exception handling in the DBI calls (prepare, execute). try adding something like:
    $sth->execute($i, $data{$i}{cname}, $j, $data{$i}{ports}{$j}{service}, + $data{$i}{ports}{$j}{banner}) or die "Content-type: text/plain\n\n $ +DBI::errstr";
    (the prepare is also an excellent candidate as well)
      %attrib = ( RaiseError => 1, PrintError => 0 );

      When you add \%attrib to the end of the DBI->connect statement (as is done) errors are handled by a die automatically. It saves you writing error checking code for every statement.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://159530]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-20 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found