Perl-CGI and MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idorjee
    New Member
    • Mar 2007
    • 76

    Perl-CGI and MySQL

    Hi guys,
    I have a small problem with my Perl-CGI script, and I would appreciate if anyone can help me out with this. I'm trying to get this done in two stages. 1st to get a bunch of info from the web and display it (which is working fine), but the 2nd part is, I want to add some of these info into my MySQL table after viewing them (I don't know how to do in Perl). Right now the script immediately adds values to the MySQL table, before I even click on submit button.
    Thanks alot in advance.
    Following is the script that I'm trying to write.

    [code=perl]

    #!/usr/bin/perl -w
    use strict;
    use CGI;
    use LWP::Simple;
    use Bio::DB::SwissP rot;
    use DBI;

    my $cgi = new CGI;

    print $cgi->header() .
    $cgi->start_html( -title => 'Results',
    -bgcolor => '#6699CC').
    # HTML table starts...
    print '<TABLE border="1" cellspacing="0" cellpadding="3" width="80%">' . "\n";
    ......
    print "<tr><th width='15%'>Ent ry Name</th><td>" . $id . "</td></tr>\n";
    print "<tr><th>Access ion</th><td>" . $accession . "</td></tr>\n";
    print "<tr><th>Protei n Name</th><td>" . $sequence . "</td></tr>\n";
    ......

    print "<tr><td><I NPUT type=\"reset\" name=\"cancel\" value=\"Cancel\ "></td>"; print "<td><INPUT type=\"submit\" name=\"update\" value=\"Update\ " onsubmit=\"???\ "></td></tr>";

    ....
    my $dbh = DBI->connect ("DBI:mysql:dat abase:xxx.xxx.x xx.xxx","root", "password") or die "Error: $DBI::errstr\n" ;
    my $sql = "INSERT INTO table (a, b, c) VALUES ('$accession', '$sequence', '$id')";
    my $in = $dbh->prepare($sql );
    $in->execute;
    [/code]
    Last edited by numberwhun; Oct 4 '07, 09:43 PM. Reason: add code tags
  • kaioshin00
    New Member
    • Nov 2006
    • 46

    #2
    I'm a beginner at perl, and have never used it for CGI development, but I think I see your problem -- your code is adding to the database right below displaying them.

    You can set up your page as a form


    [display values]

    <form action="insert. cgi">
    <input type = "submit">
    </form>


    Then, in insert.cgi, you add the values to your mySQL database.

    Or something of that nature ... since I've never used perl for CGI :)

    Comment

    • idorjee
      New Member
      • Mar 2007
      • 76

      #3
      thanks kaioshin00 for your reply.
      does anyone know why i can't execute the subroutine like this, and any other ways?:

      [code=perl]
      print $cgi->button(-name=>'update', -value=>'UPDATE' , -onClick=>&updat e_db);

      sub update_db{
      my $dbh = DBI->connect ("DBI:mysql:... ...);
      my $sql = "INSERT INTO .......
      }
      [/code]

      Appreciate it!
      Last edited by numberwhun; Oct 5 '07, 09:43 PM. Reason: add code tags

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by idorjee
        thanks kaioshin00 for your reply.
        does anyone know why i can't execute the subroutine like this, and any other ways?:

        [code=perl]
        print $cgi->button(-name=>'update', -value=>'UPDATE' , -onClick=>&updat e_db);

        sub update_db{
        my $dbh = DBI->connect ("DBI:mysql:... ...);
        my $sql = "INSERT INTO .......
        }
        [/code]

        Appreciate it!
        Can we please learn how to use code tags?! There is a sample in the REPLY GUIDELINES to the right of the message window when posting. Please read it and follow the example.

        Thank you!

        -Moderator

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by idorjee
          thanks kaioshin00 for your reply.
          does anyone know why i can't execute the subroutine like this, and any other ways?:

          [code=perl]
          print $cgi->button(-name=>'update', -value=>'UPDATE' , -onClick=>&updat e_db);

          sub update_db{
          my $dbh = DBI->connect ("DBI:mysql:... ...);
          my $sql = "INSERT INTO .......
          }
          [/code]

          Appreciate it!
          onClick is to initiate javascript code, not perl code.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by numberwhun
            Can we please learn how to use code tags?! There is a sample in the REPLY GUIDELINES to the right of the message window when posting. Please read it and follow the example.

            Thank you!

            -Moderator
            Looks like MIller left you hanging. Give yourself a break Jeff, don't worry about the code tags, what's Mary gonna do, fire you? ;)

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by KevinADC
              Looks like MIller left you hanging. Give yourself a break Jeff, don't worry about the code tags, what's Mary gonna do, fire you? ;)
              Nah, not in my nature. I am too anal to let it go. :-)

              What did happen to Miller? I haven't seen him. Haven't had a response to PM's. Mary says she saw him online but no hide nor hair.

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by numberwhun
                Nah, not in my nature. I am too anal to let it go. :-)

                What did happen to Miller? I haven't seen him. Haven't had a response to PM's. Mary says she saw him online but no hide nor hair.
                hehehe...

                I have no clue what happend to M. I hope he's on a tropical island dancing naked with the beautiful natives. I'll drop him an email and see if he responds. He might have just got burned out with the whole forum posting stuff, it happens.

                Comment

                • numberwhun
                  Recognized Expert Moderator Specialist
                  • May 2007
                  • 3467

                  #9
                  Originally posted by KevinADC
                  hehehe...

                  I have no clue what happend to M. I hope he's on a tropical island dancing naked with the beautiful natives. I'll drop him an email and see if he responds. He might have just got burned out with the whole forum posting stuff, it happens.
                  True, it certainly does! Let me know if you hear from him. Just want to make sure he's ok.

                  Comment

                  • idorjee
                    New Member
                    • Mar 2007
                    • 76

                    #10
                    heheheeee....yo u guys are funny.
                    happy chatting!

                    Comment

                    Working...