Find the answer to your Linux question:
Results 1 to 9 of 9
I am writing some forms for work which query a mysql db, anyway I created 1 form which works but I am having problems with the second one. It won't ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994

    Problem creating a new oject with cgi.pm


    I am writing some forms for work which query a mysql db, anyway I created 1 form which works but I am having problems with the second one. It won't create the new object and I don't know why. There errors are this:
    Use of uninitialized value in string eq
    Not a CODE reference

    Here is some of the code:

    Code:
    #!/usr/local/bin/perl -w
    use CGI;
    use CGI::Pretty qw( :html3 );
    use strict;
    use DBI;
    
    my %mysql= (
            server => 'localhost',
            user => 'cert',
            pass => ''XXXX'
            db => 'cert' );
    
    my @pages=("add", "results");
    my $q = new CGI;  #this doesnt get done
    my $a = $q -> param("page"); #so this scalar doenst have a value
    my $b = $q -> param("go");  #nor this one
                                                                                    
    my $currentpage = getpage($a, $b);  #causes the first error
    my $pagename = $pages[$currentpage];
                                                                                   $addpage($currentpage) if $pagename eq "add"; #2nd error happens in herre when i try to call $q
    resultpage($currentpage) if $pagename eq "results";
    
    sub addpage {blah blah blah blah;}
    
    sub resultpage {blah blah blah blah;}
    
    sub getpage {
     my ($prev,$dir) = @_;
     return 0 if $prev eq '';  #errors here cause $prev has no value
     return $prev+1 if ($dir eq "submit");;
    }

  2. #2
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    Also I coped and pasted lot of the code from my first form which is why I don't understand why it doesn't work.

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I guess you're the most knowledgable one around when it comes to perl, but isn't there a way that you can get more debugging information from cgi.pm, like recompiling it with some debugging option or gdb it or anything? Otherwise, can't it just be some mismatch between POST and GET params?

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    I think your real problem is not that you cannot create an instance of CGI ... the problem is that you are trying to use the param 'page' without checking that it has been created.

    This is an quick fix that may or may not work with your script, but it will demonstrate the problem I was referring to.

    Code:
    #!/usr/local/bin/perl -w
    
    use CGI;
    use CGI::Pretty qw( :html3 );
    use strict;
    use DBI;
    
    my %mysql= (
            server => 'localhost',
            user => 'cert',
            pass => 'XXXX',
            db => 'cert' );
    
    my @pages=("add", "results");
    my $q = new CGI(); 
    
    warn("CGI: See I am here because I can return ",$q->start_Tr(),"\n"); ## There is a CGI object because you can use this method
    
    my $a = $q -> param("page") if($q->param("page")); #only use if it is defined
    my $b = $q -> param("go") if($q->param("go"));  #only use if it is defined
    
    if($a && $b) {  ## if both paramters are available ( defined )
      my $currentpage = getpage($a, $b);  # no error here because it will not get passed undefined vars
      my $pagename = $pages[$currentpage];
      addpage($currentpage) if $pagename eq "add"; #no longer a problem  resultpage($currentpage) if $pagename eq "results";
    } else {
      warn('either $a or $b is was not defined ( or was zero ... )',"\n");
    }
    
    sub addpage {}
    
    sub resultpage {}
    
    sub getpage {
     my ($prev,$dir) = @_;
     return 0 if $prev eq '';  #errors here cause $prev has no value
     return $prev+1 if ($dir eq "submit");;
    }

    Also some quick things I noticed in the post.
    you have two errors right here:

    Code:
    my %mysql= (
            server => 'localhost',
            user => 'cert',
            pass => ''XXXX'
            db => 'cert' );
    first you have ''XXXX' so that leaves an open quotation pair. Then you are missing a comma after that.

    Code:
    my %mysql= (
            server => 'localhost',
            user => 'cert',
            pass => 'XXXX',
            db => 'cert' );
    your addpage call has a $ on it. So the perl interpreter thinks it's a variable.


    most of that may have just been typo's posting ...
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  6. #5
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    Those were juste some mistypes when i was editing the mysql password. Anyway if that is the case, why would it work on my other forms? I copied and pasted the top part over to this new form. I was messing around some more and noticed if I have my first sub there, it will work for some reason. If I remove this sub from my form that works, I get the same errors. Here it is:

    Code:
    sub searchpage {   my $currentpage = shift;                                                                                             
      print
        $q->header(),
        $q->start_html(-title=>"Tasking/Advisory"),
        $q->h1("Tasking/Advisory Query"),
        $q->start_form,
        $q->p("Choose tasking or advisory"),
        $q->radio_group (
                    -name=> 'table',
                    -values=> ['tasking','advisory'],
                    -defaults=> 'tasking'),
        $q->br(),
        $q->p("View open, closed, or either"),     
        $q->radio_group ( -name=> 'status', -values=> ['open','closed']),
        $q->br();
                                                                                                
      print
        $q->hidden(-name=>"page", -value=>$currentpage, -override=>1),
        $q->submit(-name=>"go", -value=>"submit"),
        $q->endform(),
        $q->end_html();
    }

  7. #6
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    That subroutine is the one that creates the 'page' and 'go' parameters.
    Code:
     
        $q->hidden(-name=>"page", -value=>$currentpage, -override=>1),
        $q->submit(-name=>"go", -value=>"submit"),
    Whenever I have written CGI scripts ( very few, I do mostly backend database stuff ), I have always had to check for the availability of a param that I know will be there every time the form is submitted. Of course this is when you are using the CGI script to present the form. That is what it looks like you are doing with your 'searchpage()' routine.
    Code:
    if($cgi->param('was_submitted') {
      workWithTheFormVars();
    } else {
      displayForm();
    }
    Anyway if that is the case, why would it work on my other forms?
    The only answer I have for that is that your other forms must pass these params to the script. Have you tried running your other scripts from the command line with no arguments to see if the same thing happens?

    Did the changes I made not work correctly?
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  8. #7
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    I mostly do backend work as well, this is my 2nd form I wrote using cgi.pm. I looked over your fix but it will never run since $a and $b will never be valid. I ran the form that I have working from console and it displayed the correct html so I don't know what is going on. Reason I think its not creating an object is because I get this error: Not a CODE reference at ./add.pl line 33.
    Which is the first time I use $q from within a sub. Anyway I guess I will keep hacking at this or find another way to determine what page you are on and where to go. Still has me completely stumped as to why it works in 1 form and not another.

  9. #8
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    Shot in the dark: initialize your $q with 'our' instead of 'my' ...
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  10. #9
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    Tried our but made no difference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •