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

I am working on a project for class and I am having some trouble getting my data inserted into the database properly. First, data is input into a form, and upon submit a new script to insert that data entered into the tables is executed. I fairly certain I know where the problem is (bold in code) but I don't know how to fix it.

The code queries different tables in the database to present drop down boxes of valid selections. That works properly, I have tested it.

<form name="ClassInfo" method="post" action="inputclass.cgi"> <h3 class="style1">Class Information</h3> [B]<p class="style1">Instructor:&nbsp; <select name="teacher" style="w +idth: 150px"> <option></option> EOF $statement1 = "Select emp_ID, fname, lname From employees Wher +e is_prof=1"; $sth1 = $dbh->prepare($statement1); $sth1->execute; my %row; $sth1->bind_columns( \( @row{ @{$sth1->{NAME_lc} } } )); while($sth1->fetch){ print"<option value=\"$row{emp_id}\">$row{fname} &nbsp; $r +ow{lname}</option>"; } print <<EOF; </select></p> <p class="style1">Sales Person:&nbsp; <select name="salesperson" style +="width: 150px"> <option></option> EOF $statement2 = "Select emp_ID as emp_id2, fname as fname2, lnam +e as lname2 From employees Where is_salerep=1"; $sth2 = $dbh->prepare($statement2); $sth2->execute; my %row; $sth2->bind_columns( \( @row{ @{$sth2->{NAME_lc} } } )); while($sth2->fetch){ print"<option value=\"$row{emp_id2}\">$row{fname2} &nbsp; +$row{lname2}</option>"; } print <<EOF; </select></p> <p class="style1">Course:&nbsp; <select name="course" style="width: 15 +0px"> <option></option> EOF $statement3 = "Select course_ID, cname From courses"; $sth3 = $dbh->prepare($statement3); $sth3->execute; my %row; $sth3->bind_columns( \( @row{ @{$sth3->{NAME_lc} } } )); while($sth3->fetch){ print"<option value=\"$row{course_id}\">$row{cname}</optio +n>"; } print <<EOF; </select></p> <p class="style1">Buyer:&nbsp; <select name="comid" style="width: 150p +x"> <option></option> EOF $statement4 = "Select com_ID, cname as comname From companies" +; $sth4 = $dbh->prepare($statement4); $sth4->execute; my %row; $sth4->bind_columns( \( @row{ @{$sth4->{NAME_lc} } } )); while($sth4->fetch){ print"<option value=\"$row{com_id}\">$row{comname}</option +>"; }[/B] print <<EOF; </select></p> <p class="style1">Date Sold (YYYY/MM/DD):&nbsp; <input name="ds" type="text" style="width: 101px" size="5" /> </p> <p class="style1">Price Sold For:&nbsp; <input name="ps" type="text" style="width: 101px" size="5" /> </p>

Here I insert the data received above as a new entry into the three tables shown below. The addresses table entry is built properly. The problem lies somewhere within the classes table because the entry does not populate. I feel undefined data is being sent by one of the columns.

$dbh->do("Insert into addresses values ($aid, '$formValues{'street1'}', '$formValues{'street2'}', '$fo +rmValues{'city'}', '$formValues{'state'}', $formValues{'zip'})"); print"<p>finished addresses</p>"; $dbh->do("Insert into classes values ($clid, $formValues{'teacher'}, $formValues{'course'}, $aid, $f +ormValues{'sd'}, $formValues{'st'}, $formValues{'ed'}, $formValues{'et'})"); print"<p>finished classes</p>"; $dbh->do("Insert into sell values ($tid, $formValues{'salesperson'}, $clid, $formValues{'comid'}, + $formValues{'ps'}, '$formValues{'ds'}')");

formValues comes from a .pm file code to parse is:

sub parseForm { local($in, %in) ; local($name, $value) ; # First, read entire string of CGI vars into $in if ( ($ENV{'REQUEST_METHOD'} eq 'GET') || ($ENV{'REQUEST_METHOD'} eq 'HEAD') ) { $in= $ENV{'QUERY_STRING'} ; } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencode +d$#i) { $ENV{'CONTENT_LENGTH'} || &HTMLdie("No Content-Length sent with the POST requ +est.") ; read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ; } else { &HTMLdie("Unsupported Content-Type: $ENV{'CONTENT_TYPE'}") + ; } } else { &HTMLdie("Script was called with unsupported REQUEST_METHOD.") + ; }

with command (perl -w #filename) the error message returned is: Script was called with unsupported REQUEST_METHOD. I built the form with post... and post is a valid request method. I am also told: (Use of uninitialized value in string eq at functions.pm line 23.) line 23 is ( if ( ($ENV{'REQUEST_METHOD'} eq 'GET') ||)

for more information is needed, I will be glad to provide it! Thanks all!

Replies are listed 'Best First'.
Re: unsupported REQUEST_METHOD?
by zwon (Abbot) on Nov 14, 2009 at 21:47 UTC

    How do you run this script? It looks like REQUEST_METHOD environment variable isn't set. If you run this script from command line you should set it manually. Try print out environment somewhere in your script:

    use Data::Dumper; warn Dumper \%ENV;
    it will print list of environment variables into web-server error log, or to console if you run it from command line.

      Jtm62 here...

      I'm not sure but I think the script is excuted in the web browser when I click submit on the form in the first script I posted. I haven't had this problem with any of the other input forms I have built for the project. This is the only one that is giving me a fit. This is my first time experiencing Perl, and while I am not bad at debugging it, this problem has proved to be more than I could manage. I'll try executing it in the command line, it may shed some more light on the issue, but these scripts are supposed to be run online.

      thanks for you help!

Re: unsupported REQUEST_METHOD?
by zentara (Cardinal) on Nov 15, 2009 at 14:21 UTC
    .... as to your line 23 problem, it may be an if-elsif-else logic problem

    ...try commenting out all your logic, and try just

    if ($ENV{'REQUEST_METHOD'} eq 'POST'){ print "success\n" }
    ...these lines of yours look particularly odd, as far as possible logic and prescendence errors
    elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencode +d$#i) { $ENV{'CONTENT_LENGTH'} || &HTMLdie("No Content-Length sent with the POST requ +est.") ; read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ; } else {
    its too early sunday morning for me to test that logic out.... but that is where i would look first

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: unsupported REQUEST_METHOD?
by Anonymous Monk on Nov 15, 2009 at 15:16 UTC

      I'm just using what was provided to us in class. The project focus is on the database design and queries, not the interface to go with it. I know nothing about CGI or CGI::Simple, maybe I'll look into it.

      Thank you for the suggestion.

        If you want to focus on database design and queries use CGI, parseForm is a complete waste of time. Hand parsing CGI has been a bad idea for 16 years now, since 1993, really. Tell your classmates, tell your teacher, use CGI or die;
Re: unsupported REQUEST_METHOD?
by jtm62 (Initiate) on Nov 15, 2009 at 17:50 UTC

    Furthermore, form works and all the data is brought to the inputclass.cgi script. I used a print statement to ensure everything is there. It even goes so far as to actually insert the values into the 'addresses' table, however, it fails to insert data into the 'class' or 'sell' table.