LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CGI MySql HTML Forms (https://www.linuxquestions.org/questions/programming-9/cgi-mysql-html-forms-791721/)

kantu 02-26-2010 07:04 AM

CGI MySql HTML Forms
 
I was trying to get data from html forms and then insert it into sql database
Code:

my $st=param('student');
my $reg=param('regno');
my $marks=param('marks');
.
.
.
my $sth1 = $dbh->prepare('insert into stdb(name,regno,marks) values ('$st','$reg','$marks')');

I am stuck in that insert query , i googled it out and saw on some articles that we can use %FORM{name} etc instead of using $st in my case
But i got errors for that also
Global symbol "%FORM" requires explicit package name
I am new to perl scripting, i tried perldocs and perldoc site also but found it tough to understand, could some one please guide me or suggest a beginner book ?

TB0ne 02-26-2010 10:30 AM

Quote:

Originally Posted by kantu (Post 3877706)
I was trying to get data from html forms and then insert it into sql database
Code:

my $st=param('student');
my $reg=param('regno');
my $marks=param('marks');
.
.
.
my $sth1 = $dbh->prepare('insert into stdb(name,regno,marks) values ('$st','$reg','$marks')');

I am stuck in that insert query , i googled it out and saw on some articles that we can use %FORM{name} etc instead of using $st in my case
But i got errors for that also
Global symbol "%FORM" requires explicit package name
I am new to perl scripting, i tried perldocs and perldoc site also but found it tough to understand, could some one please guide me or suggest a beginner book ?

So you got errors...how about telling us what the errors were. We can't guess.

But if that's your code, you seem to be missing the part where you open the connection to MySQL, and attach to the database.
Code:

my $dsn = "DBI:mysql:<Database Name Goes Here>";
my $user = "<DB User name, either as string or variable>";
my $pass = "<DB password, either as string or variable>";
$dbh = DBI->connect($dsn, $user, $pass,{ RaiseError => 1})
    or die "Could not connect to database! $DBI::errstr";

And your insert line looks wrong:
Code:

$dbh->do("INSERT INTO <Table Name> values (\"$variable1\",\"$variable2\")");

kantu 02-26-2010 11:18 PM

solved
 
I didnt put the whole code i only put the line where i got stuck ,
instead of having query in dbh->prepare
I had a variable that holds my query ,
$query="INSERT INTO table values('$val1','$val2')";
now dbh->prepare($query)
This worked fine now , iam trying to figure out why it cant be done straight forward as in my first post


All times are GMT -5. The time now is 01:48 AM.