473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 6677
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

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

Similar topics

0
by: richat | last post by:
hi everyone ,i hav this problm that this code doesn't insert data into the database. It says that active perl not installed or some error in the code .plz help me out .Its urgent. ...
0
by: troydixon | last post by:
Hello, I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the...
2
by: S_K | last post by:
Hi, I have a problem in which I have to insert data from an ASP.NET form into an Adobe (.pdf) form. How can I do this? Thanks. Steve
66
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it...
1
by: likun12 | last post by:
i develope a barcode program from that barcode is generated.but i just dont know how to insert data into sqlserver from the barcode reader.
0
by: brianrpsgt1 | last post by:
I am attempting to insert data from a HTML form using a .psp script. I can not find how to link the data that is inserted into the form to the variables in the .psp script to then insert into the...
3
by: happyse27 | last post by:
Hi All, I am creating the perl script using html form(with embedded javascript inside). When using this html form with javascript alone, it works where the form validation will pop up...
1
pradeepjain
by: pradeepjain | last post by:
Hii guys, I have 2 tables in which data exists of a same user like his table1:login details and table2: his further details . I have a form where in his both login and rest of details...
1
by: krus | last post by:
Hi All, I am relatively new to HTML and perl scripts. I am writing a HTML page which has a form. It takes two inputs. Now I want pass these two inputs from the HTML form to the perl script. I want...
2
by: pavelcc | last post by:
I need to insert data into a table after validating that the data doesn't already exist on the table. In addition to that I need to update data if there are any changes. I got the insert part...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.