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 - # 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 - <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>
15 6677 RonB 589
Expert Mod 512MB -
$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.
- #!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!
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. - 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. -
#!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;
-
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. - #!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!
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. - print $cgi->p("[$head] [$subhead]");
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: - #!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;
-
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?
Hi,
simply how to do this?
Thank You!
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?
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: - #!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;
-
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.
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. - #!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;
-
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
...
|
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...
|
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
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |