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

Hi all,

I have a Following code:

#! c:/perl/bin/perl.exe use CGI qw(:standard); use DBI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); $dbh = DBI->connect("DBI:mysql:database=perldbi;host=localhost","root" +,""); $sth = $dbh->prepare('select * from test'); $sth->execute(); print header; print start_html('Newgen Imaging Systems(P) Ltd.,'); print h1("Hello world"); if($sth->rows == 0) { print h1("YES"); } else { print h4("This is available table\n"); print "<table border=2>\n"; while( $resptr = $sth->fetchrow_hashref()) { print "<tr>"; print "<td>". $resptr->{"name"}; print "<td>". $resptr->{"pass"}; print "\n"; print "<\/tr>"; } print "<\/table>\n"; } print "<br><input type='text' name='usrtxt'>"; print "<br><input type='text' name='passtxt'>"; print "<br><input type='submit' name='logbtn' value ='Insert'>"; print "\t\t"; print "<input type='submit' name='logbtn' value ='Clear'>"; $dbh->disconnect; print end_html;

My doubt is:

If am click the button 'logbtn' then the values in the two text box is store into the database

Anyone please help me

20070407 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Perl CGI Database Connectivity
by friedo (Prior) on Apr 07, 2007 at 07:57 UTC

    To get the value of your form parameters, use CGI's param method, e.g.

    my $submit = param( 'logbtn' ); if ( $submit eq 'Insert' ) { # insert into database here } else { # display form here }

    To make your code cleaner, you may want to look into a templating system like HTML::Template or Template Toolkit. And if this is going to be a complicated application with many screens, there are frameworks available that let you avoid writing a huge tree of if/else blocks. My favorite is CGI::Application.

    Update: You really should also be using strict and warnings is a good idea too.

Re: Perl CGI Database Connectivity
by talexb (Chancellor) on Apr 07, 2007 at 16:49 UTC

    I notice you're logging into the database as root -- that's not really a good habit to get into. Set up a development account with normal privileges, or even one per project that you're working on.

    You'll thank yourself later when you accidentally drop an important table and get told, "Silly goose, you can't do that!" because the privileges prevented you from doing that.

    And I echo the earlier comments about templating -- a bit longer to set up, vastly easier to develop with, and terrific separation between logic and presentation.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Perl CGI Database Connectivity
by TOD (Friar) on Apr 07, 2007 at 11:40 UTC
    first, i don't see any line of code in which you store the input values resulting from the html form. second, you might get in troubles with firefox, which sometimes constructs false name=value pairs from named submit buttons.
      you might get in troubles with firefox, which sometimes constructs false name=value pairs from named submit buttons.
      It does? I've been using that construct for years, and AFAIK it's supported by every brower.

      Update: ofcourse, since there's no <FORM> tag the whole code will probably not work at all in numerous browsers.