insert data into mysql table by the help of html form and perl script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ruchi choudhary
    New Member
    • Nov 2012
    • 22

    insert data into mysql table by the help of html form and perl script

    Hello,

    I want to insert data through html page. So for that I created two file one is for html page(insertdis. html) and second one is for perl script(insert1. pl).But it could not work. Is it correct way for inserting data in database?
    Another option of using PERL-CGI but I do not know about this language so please kindly help me.

    Thanks!
    insert1.pl
    Code:
    # PERL MODULES WE WILL BE USING
    use DBI;
    use DBD::mysql;
    # CONFIG VARIABLES
    $platform = "mysql";
    $database = "purchase";
    $port = "3306";
    $host = "localhost";
    $user = "root";
    $pw = "";
    #DATA SOURCE NAME
    $dsn = "dbi:mysql:$database:localhost:3306";
    # PERL DBI CONNECT
    $DBIconnect = DBI->connect($dsn, $user, $pw)or die "unable to connect:$DBI::errstr\n";
    
    $head=$_POST['head'];
    $subhead=$_POST['subhead'];
    #PREPARE THE QUERY
    $query = "INSERT INTO naipone (head, subhead) VALUES ('$head','$subhead')";
    $query_handle = $DBIconnect->prepare($query); 
    # EXECUTE THE QUERY
    $query_handle->execute();
    insertdis.html

    Code:
    <html>
    <head>
    <body text="#FFFFFF" bgcolor="#800000">
    <form method="post" action="insert1.pl">
    head:<br/>
    <input type="text" name="head" size="30"/><br/>
    subhead:<br/>
    <input type="text" name="subhead" size="30"/><br/>
    
    <input type="submit" value="update" size="30"/>
    </form>
    </body>
    </html>
    Last edited by Rabbit; Feb 16 '13, 09:30 AM. Reason: Please use code tags when posting code.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Code:
    $head=$_POST['head'];
    $subhead=$_POST['subhead'];
    That is php syntax, not perl.

    You need to use the CGI module to retrieve/parse the form submission.

    Comment

    • Ruchi choudhary
      New Member
      • Nov 2012
      • 22

      #3
      Code:
      #!C:\wamp\bin\perl\bin\perl.exe -wT
      print "ContentType: text/html\n\n";
      # PERL MODULES WE WILL BE USING
      use DBI;
      use DBD::mysql;
      my $q = CGI->new;
      # CONFIG VARIABLES
      $platform = "mysql";
      $database = "purchase";
      $port = "3306";
      $host = "localhost";
      $user = "root";
      $pw = "";
      #DATA SOURCE NAME
      $dsn = "dbi:mysql:$database:localhost:3306";
      # PERL DBI CONNECT
      $DBIconnect = DBI->connect($dsn, $user, $pw)or die "unable to connect:$DBI::errstr\n";
      
      my $head=$q->param['head'];
      my $subhead=$q->param['subhead'];
      #PREPARE THE QUERY
      $query = "INSERT INTO naipone (head, subhead) VALUES ('$head','$subhead')";
      $query_handle = $DBIconnect->prepare($query); 
      # EXECUTE THE QUERY
      $query_handle->execute();
      Actually I have no idea about CGI.I am completly new to CGI.I studied a lot about CGI but still unable to insert data into mysql database.Can you suggest me the code og perl-cgi for insertion?
      And its urgent.So please help me.
      Thank you!

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Lines 19 and 20 still have a syntax problem. $q->param is a function call and functions use ( ) parens around their args, not brackets.
        Code:
        my $head=$q->param('head');
        my $subhead=$q->param('subhead');
        Every Perl script you write should begin by loading the strict and warnings pragmas. They will point out lots of common mistakes.

        When developing cgi scripts, you should also include the CGI::Carp module. It redirects the error and warning messages to the browser, which makes it easier to troubleshoot the script. You can/should remove that module when you're ready to put the script into production.

        Your print statement for the content-type is "ok", but it would be better to use the method provided by the CGI module.

        Code:
        #!C:\wamp\bin\perl\bin\perl.exe -T
        
        use strict;
        use warnings FATAL => 'all';
        use CGI::Carp qw(fatalsToBrowser);
        use DBI;
        use DBD::mysql;
        
        my $cgi = CGI->new;
        print $cgi->header, $cgi->start_html;
        
        my $head     = $cgi->param('head');
        my $subhead  = $cgi->param('subhead');
        my $database = "purchase";
        my $host     = "localhost";
        my $user     = "root";
        my $pw       = "";
        my $dsn      = "dbi:mysql:$database:localhost:3306";
        my $dbh      = DBI->connect($dsn, $user, $pw,
                                   { RaiseError => 1 })
                    or die "unable to connect:$DBI::errstr\n";
         
        my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
        my $sth   = $dbh->prepare($query); 
        
        $sth->execute($head, $subhead);
        
        # additional processing as needed ...
        
        print $cgi->end_html;

        Comment

        • Ruchi choudhary
          New Member
          • Nov 2012
          • 22

          #5
          Thank you very much for your positive response.But now i get this error
          DBD::mysql::st execute failed: Column 'head' cannot be null at C:/wamp/bin/apache/Apache2.2.21/cgi-bin/insert4.pl line 22.

          Code:
          #!C:\wamp\bin\perl\bin\perl.exe -T
          use strict;
          use warnings FATAL => 'all';
          use CGI;
          use CGI::Carp qw(fatalsToBrowser);
          use DBI;
          use DBD::mysql;
          
          my $cgi = CGI->new;
          print $cgi->header, $cgi->start_html;
          my $head     = $cgi->param('head');
          my $subhead  = $cgi->param('subhead');
          my $database = "purchase";
          my $host     = "localhost";
          my $user     = "root";
          my $pw       = "";
          my $dsn      = "dbi:mysql:$database:localhost:3306";
          my $dbh      = DBI->connect($dsn, $user, $pw,
                                     { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
          my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
          my $sth   = $dbh->prepare($query); 
          $sth->execute($head, $subhead);
           
          # additional processing as needed ...
           
          print $cgi->end_html;
          Thank you!

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            Did you enter a value in both form fields?

            Add this prior to the db statements to verify what the param values hold.
            Code:
            print $cgi->p("[$head] [$subhead]");

            Comment

            • Ruchi choudhary
              New Member
              • Nov 2012
              • 22

              #7
              Actually I have been worked on php for database developing.So like that ,I want to give the input from html form then its insert into mysql database by perl-CGI.Is it possible?
              Here is my code:
              Code:
              #!C:\wamp\bin\perl\bin\perl.exe -T
              
              use warnings FATAL => 'all';
              use CGI;
              use CGI::Carp qw(fatalsToBrowser);
              use DBI;
              use DBD::mysql;
              
              my $cgi = CGI->new;
              
              print $cgi->header, $cgi->start_html;
              print $cgi->p("[$head] [$subhead]");
              my $head     = $cgi->param('head');
              my $subhead  = $cgi->param('subhead');
              my $database = "purchase";
              my $host     = "localhost";
              my $user     = "root";
              my $pw       = "";
              my $dsn      = "dbi:mysql:$database:localhost:3306";
              my $dbh      = DBI->connect($dsn, $user, $pw,
                                         { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
              print $cgi->p("[$head] [$subhead]");
              my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
              my $sth   = $dbh->prepare($query); 
              $sth->execute($head, $subhead);
               
              # additional processing as needed ...
               
              print $cgi->end_html;

              Comment

              • RonB
                Recognized Expert Contributor
                • Jun 2009
                • 589

                #8
                I want to give the input from html form then its insert into mysql database by perl-CGI.Is it possible?
                Yes it is possible.

                And your next question is?

                Comment

                • Ruchi choudhary
                  New Member
                  • Nov 2012
                  • 22

                  #9
                  Hi,
                  simply how to do this?
                  Thank You!

                  Comment

                  • RonB
                    Recognized Expert Contributor
                    • Jun 2009
                    • 589

                    #10
                    The code I already posted shows you how to do it.

                    If it's not working for you, you'll need to provide info on how it's failing. What error messages are you receiving?

                    Comment

                    • Ruchi choudhary
                      New Member
                      • Nov 2012
                      • 22

                      #11
                      ERROR: Use of uninitialized value $head in concatenation (.) or string at C:/wamp/bin/apache/Apache2.2.21/cgi-bin/insert4.pl line 12.

                      Code is:

                      Code:
                      #!C:\wamp\bin\perl\bin\perl.exe -T
                      
                      use warnings FATAL => 'all';
                      use CGI;
                      use CGI::Carp qw(fatalsToBrowser);
                      use DBI;
                      use DBD::mysql;
                      
                      my $cgi = CGI->new;
                      
                      print $cgi->header, $cgi->start_html;
                      print $cgi->p("[$head] [$subhead]");
                      my $head     = $cgi->param('head');
                      my $subhead  = $cgi->param('subhead');
                      my $database = "purchase";
                      my $host     = "localhost";
                      my $user     = "root";
                      my $pw       = "";
                      my $dsn      = "dbi:mysql:$database:localhost:3306";
                      my $dbh      = DBI->connect($dsn, $user, $pw,
                                                 { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
                      print $cgi->p("[$head] [$subhead]");
                      my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
                      my $sth   = $dbh->prepare($query); 
                      $sth->execute($head, $subhead);
                       
                      # additional processing as needed ...
                       
                      print $cgi->end_html;

                      Comment

                      • RonB
                        Recognized Expert Contributor
                        • Jun 2009
                        • 589

                        #12
                        You need to retrieve the form field values before you can print them, so move that print statement (line 12) down a few rows.

                        Comment

                        • Ruchi choudhary
                          New Member
                          • Nov 2012
                          • 22

                          #13
                          I do this but it does not work and seriously I can't understand Perl-CGI at all.

                          Error: DBD::mysql::st execute failed: Column 'head' cannot be null at C:/wamp/bin/apache/Apache2.2.21/cgi-bin/insert4.pl line 25.

                          Code:
                          #!C:\wamp\bin\perl\bin\perl.exe -T
                          
                          use warnings FATAL => 'all';
                          use CGI;
                          use CGI::Carp qw(fatalsToBrowser);
                          use DBI;
                          use DBD::mysql;
                          
                          my $cgi = CGI->new;
                          
                          print $cgi->header, $cgi->start_html;
                          
                          my $head     = $cgi->param('head');
                          my $subhead  = $cgi->param('subhead');
                          my $database = "purchase";
                          my $host     = "localhost";
                          my $user     = "root";
                          my $pw       = "";
                          my $dsn      = "dbi:mysql:$database:localhost:3306";
                          my $dbh      = DBI->connect($dsn, $user, $pw,
                                                     { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
                          
                          my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
                          my $sth   = $dbh->prepare($query); 
                          $sth->execute($head, $subhead);
                          print $cgi->p("[$head] [$subhead]");
                           
                          print $cgi->end_html;

                          Comment

                          • RonB
                            Recognized Expert Contributor
                            • Jun 2009
                            • 589

                            #14
                            What part of the perl code do you not understand?

                            The problem is not with the perl coding. The problem is on the html side.

                            The error message is telling you that $head does not have a value i.e. it's undefined which equates to null in mysql and your database is configured to not allow that field to be null. The most likely cause of it being undefined is that you didn't enter anything in the "head" text field of the html form.

                            Comment

                            • Ruchi choudhary
                              New Member
                              • Nov 2012
                              • 22

                              #15
                              you means I have to create a html page in which I have to call this file,so this html form where should be place in cgi-bin folder or it may be in www folder of wamp server but by both ways it does not work.

                              Comment

                              Working...