in reply to Method "do" error

You're declaring $dbh (which results in a new variable), and then immediately calling do on it! Of course it'd be undef. You'll need to do something to connect o the database before you use the database. Perhaps simply removing my $dbh from that subroutine will work, but I don't know what WebDB does.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Method "do" error
by b310 (Scribe) on Apr 20, 2003 at 19:49 UTC
    Hi,

    The WebDB is a module that connects to the database.
    I did verify that all the parameters in order to connect are correct. They are. The module works without a problem.

    All I'm trying to do is call in a few parameters from a form and insert them into a table.

      I think merlyn has already provided the answer. In the store_items subroutine, simply remove this line:

        my $dbh;

      From your description of WebDB, it seems likely that it is creating a DBI database handle object and storing it in a global variable called '$dbh'. By including 'my $dbh;' in the subroutine, you are creating a new variable with the same name that only exists within the subroutine and prevents access to the global. Because the variable in the subroutine is new, it is undefined.

      Since you have 'use strict' in effect, it is possible that removing the my $dbh; line will trigger the error: Global symbol "$dbh" requires explicit package name. If that is the case, you'll need to explicitly declare that it is a global variable like this:

        our $dbh;
        Hi,

        I tried your suggestion.

        First, I did remove the $dbh from the subrouting and received an internal server error message.
        Secondly, I also change my $dbh to our $dbh in hopes that would solve the problem. I received the same internal server error message.

        I included the modified code. Any other ideas or thoughts?

        #! /usr/local/bin/perl -Tw use strict; use lib qw(/usr163/home/w/s/wstrader/public_html/library); use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard escape escapeHTML); use WebDB; our $dbh = WebDB::connect (); my $name = param("name"); my $coname = param("coname"); my $phone = param("phone"); my $email = param("email"); my $address = param("address"); my $city = param("city"); my $state = param("state"); my $zip = param("zip"); my $country = param("country"); my $page; my $choice = lc (param ("choice")); if ($choice eq "submit") # information submitted via form { store_items (); } else { $page .= p (escapeHTML ("Logic error, unknown choice: $choice")); } sub store_items { $dbh->do ("INSERT INTO contact_info (contact_date,contact_name,company,telephone,email,addre +ss,city,state,zip_code,country) VALUES (CURRENT_DATE,?,?,?,?,?,?,?,?,?)", undef, $name, $coname, $phone, $email, $address, $city, $state, $zip, $country); $dbh->disconnect(); }