469,343 Members | 5,186 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1. # PERL MODULES WE WILL BE USING
  2. use DBI;
  3. use DBD::mysql;
  4. # CONFIG VARIABLES
  5. $platform = "mysql";
  6. $database = "purchase";
  7. $port = "3306";
  8. $host = "localhost";
  9. $user = "root";
  10. $pw = "";
  11. #DATA SOURCE NAME
  12. $dsn = "dbi:mysql:$database:localhost:3306";
  13. # PERL DBI CONNECT
  14. $DBIconnect = DBI->connect($dsn, $user, $pw)or die "unable to connect:$DBI::errstr\n";
  15.  
  16. $head=$_POST['head'];
  17. $subhead=$_POST['subhead'];
  18. #PREPARE THE QUERY
  19. $query = "INSERT INTO naipone (head, subhead) VALUES ('$head','$subhead')";
  20. $query_handle = $DBIconnect->prepare($query); 
  21. # EXECUTE THE QUERY
  22. $query_handle->execute();
insertdis.html

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <body text="#FFFFFF" bgcolor="#800000">
  4. <form method="post" action="insert1.pl">
  5. head:<br/>
  6. <input type="text" name="head" size="30"/><br/>
  7. subhead:<br/>
  8. <input type="text" name="subhead" size="30"/><br/>
  9.  
  10. <input type="submit" value="update" size="30"/>
  11. </form>
  12. </body>
  13. </html>
Feb 16 '13 #1
15 6169
RonB
589 Expert Mod 512MB
Expand|Select|Wrap|Line Numbers
  1. $head=$_POST['head'];
  2. $subhead=$_POST['subhead'];
  3.  
That is php syntax, not perl.

You need to use the CGI module to retrieve/parse the form submission.
Feb 16 '13 #2
Expand|Select|Wrap|Line Numbers
  1. #!C:\wamp\bin\perl\bin\perl.exe -wT
  2. print "ContentType: text/html\n\n";
  3. # PERL MODULES WE WILL BE USING
  4. use DBI;
  5. use DBD::mysql;
  6. my $q = CGI->new;
  7. # CONFIG VARIABLES
  8. $platform = "mysql";
  9. $database = "purchase";
  10. $port = "3306";
  11. $host = "localhost";
  12. $user = "root";
  13. $pw = "";
  14. #DATA SOURCE NAME
  15. $dsn = "dbi:mysql:$database:localhost:3306";
  16. # PERL DBI CONNECT
  17. $DBIconnect = DBI->connect($dsn, $user, $pw)or die "unable to connect:$DBI::errstr\n";
  18.  
  19. my $head=$q->param['head'];
  20. my $subhead=$q->param['subhead'];
  21. #PREPARE THE QUERY
  22. $query = "INSERT INTO naipone (head, subhead) VALUES ('$head','$subhead')";
  23. $query_handle = $DBIconnect->prepare($query); 
  24. # EXECUTE THE QUERY
  25. $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!
Feb 18 '13 #3
RonB
589 Expert Mod 512MB
Lines 19 and 20 still have a syntax problem. $q->param is a function call and functions use ( ) parens around their args, not brackets.
Expand|Select|Wrap|Line Numbers
  1. my $head=$q->param('head');
  2. 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.

Expand|Select|Wrap|Line Numbers
  1. #!C:\wamp\bin\perl\bin\perl.exe -T
  2.  
  3. use strict;
  4. use warnings FATAL => 'all';
  5. use CGI::Carp qw(fatalsToBrowser);
  6. use DBI;
  7. use DBD::mysql;
  8.  
  9. my $cgi = CGI->new;
  10. print $cgi->header, $cgi->start_html;
  11.  
  12. my $head     = $cgi->param('head');
  13. my $subhead  = $cgi->param('subhead');
  14. my $database = "purchase";
  15. my $host     = "localhost";
  16. my $user     = "root";
  17. my $pw       = "";
  18. my $dsn      = "dbi:mysql:$database:localhost:3306";
  19. my $dbh      = DBI->connect($dsn, $user, $pw,
  20.                            { RaiseError => 1 })
  21.             or die "unable to connect:$DBI::errstr\n";
  22.  
  23. my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
  24. my $sth   = $dbh->prepare($query); 
  25.  
  26. $sth->execute($head, $subhead);
  27.  
  28. # additional processing as needed ...
  29.  
  30. print $cgi->end_html;
  31.  
Feb 19 '13 #4
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.

Expand|Select|Wrap|Line Numbers
  1. #!C:\wamp\bin\perl\bin\perl.exe -T
  2. use strict;
  3. use warnings FATAL => 'all';
  4. use CGI;
  5. use CGI::Carp qw(fatalsToBrowser);
  6. use DBI;
  7. use DBD::mysql;
  8.  
  9. my $cgi = CGI->new;
  10. print $cgi->header, $cgi->start_html;
  11. my $head     = $cgi->param('head');
  12. my $subhead  = $cgi->param('subhead');
  13. my $database = "purchase";
  14. my $host     = "localhost";
  15. my $user     = "root";
  16. my $pw       = "";
  17. my $dsn      = "dbi:mysql:$database:localhost:3306";
  18. my $dbh      = DBI->connect($dsn, $user, $pw,
  19.                            { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
  20. my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
  21. my $sth   = $dbh->prepare($query); 
  22. $sth->execute($head, $subhead);
  23.  
  24. # additional processing as needed ...
  25.  
  26. print $cgi->end_html;
  27.  
Thank you!
Feb 20 '13 #5
RonB
589 Expert Mod 512MB
Did you enter a value in both form fields?

Add this prior to the db statements to verify what the param values hold.
Expand|Select|Wrap|Line Numbers
  1. print $cgi->p("[$head] [$subhead]");
Feb 20 '13 #6
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:
Expand|Select|Wrap|Line Numbers
  1. #!C:\wamp\bin\perl\bin\perl.exe -T
  2.  
  3. use warnings FATAL => 'all';
  4. use CGI;
  5. use CGI::Carp qw(fatalsToBrowser);
  6. use DBI;
  7. use DBD::mysql;
  8.  
  9. my $cgi = CGI->new;
  10.  
  11. print $cgi->header, $cgi->start_html;
  12. print $cgi->p("[$head] [$subhead]");
  13. my $head     = $cgi->param('head');
  14. my $subhead  = $cgi->param('subhead');
  15. my $database = "purchase";
  16. my $host     = "localhost";
  17. my $user     = "root";
  18. my $pw       = "";
  19. my $dsn      = "dbi:mysql:$database:localhost:3306";
  20. my $dbh      = DBI->connect($dsn, $user, $pw,
  21.                            { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
  22. print $cgi->p("[$head] [$subhead]");
  23. my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
  24. my $sth   = $dbh->prepare($query); 
  25. $sth->execute($head, $subhead);
  26.  
  27. # additional processing as needed ...
  28.  
  29. print $cgi->end_html;
  30.  
Feb 27 '13 #7
RonB
589 Expert Mod 512MB
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?
Feb 27 '13 #8
Hi,
simply how to do this?
Thank You!
Feb 28 '13 #9
RonB
589 Expert Mod 512MB
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?
Feb 28 '13 #10
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:

Expand|Select|Wrap|Line Numbers
  1. #!C:\wamp\bin\perl\bin\perl.exe -T
  2.  
  3. use warnings FATAL => 'all';
  4. use CGI;
  5. use CGI::Carp qw(fatalsToBrowser);
  6. use DBI;
  7. use DBD::mysql;
  8.  
  9. my $cgi = CGI->new;
  10.  
  11. print $cgi->header, $cgi->start_html;
  12. print $cgi->p("[$head] [$subhead]");
  13. my $head     = $cgi->param('head');
  14. my $subhead  = $cgi->param('subhead');
  15. my $database = "purchase";
  16. my $host     = "localhost";
  17. my $user     = "root";
  18. my $pw       = "";
  19. my $dsn      = "dbi:mysql:$database:localhost:3306";
  20. my $dbh      = DBI->connect($dsn, $user, $pw,
  21.                            { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
  22. print $cgi->p("[$head] [$subhead]");
  23. my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
  24. my $sth   = $dbh->prepare($query); 
  25. $sth->execute($head, $subhead);
  26.  
  27. # additional processing as needed ...
  28.  
  29. print $cgi->end_html;
  30.  
Mar 1 '13 #11
RonB
589 Expert Mod 512MB
You need to retrieve the form field values before you can print them, so move that print statement (line 12) down a few rows.
Mar 1 '13 #12
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.

Expand|Select|Wrap|Line Numbers
  1. #!C:\wamp\bin\perl\bin\perl.exe -T
  2.  
  3. use warnings FATAL => 'all';
  4. use CGI;
  5. use CGI::Carp qw(fatalsToBrowser);
  6. use DBI;
  7. use DBD::mysql;
  8.  
  9. my $cgi = CGI->new;
  10.  
  11. print $cgi->header, $cgi->start_html;
  12.  
  13. my $head     = $cgi->param('head');
  14. my $subhead  = $cgi->param('subhead');
  15. my $database = "purchase";
  16. my $host     = "localhost";
  17. my $user     = "root";
  18. my $pw       = "";
  19. my $dsn      = "dbi:mysql:$database:localhost:3306";
  20. my $dbh      = DBI->connect($dsn, $user, $pw,
  21.                            { RaiseError => 1 })        or die "unable to connect:$DBI::errstr\n";
  22.  
  23. my $query = "INSERT INTO naipone (head, subhead) VALUES (?,?)";
  24. my $sth   = $dbh->prepare($query); 
  25. $sth->execute($head, $subhead);
  26. print $cgi->p("[$head] [$subhead]");
  27.  
  28. print $cgi->end_html;
  29.  
Mar 2 '13 #13
RonB
589 Expert Mod 512MB
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.
Mar 2 '13 #14
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.
Mar 4 '13 #15
RonB
589 Expert Mod 512MB
The Perl script goes into the cgi-bin dir and the html file goes into the htdocs dir (or whatever dir is configured as your DocumentRoot).

Saying "it does not work" doesn't help either of us.
Mar 4 '13 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by S_K | last post: by
reply views Thread by brianrpsgt1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.