#!/usr/local/bin/perl
#print "Content-type: text/html\n\n";
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use DBI;
use strict;
my $query = new CGI;
my $HOST = "localhost";
my $DATABASE = "db";
my $USERNAME = "us";
my $PASSWORD = "pw";
my $dsn = "DBI:mysql:$DATABASE:$HOST";
my $DB = DBI->connect($dsn, $USERNAME, $PASSWORD, {});
print $query->header ( );
# Process form if submitted; otherwise display it
if (param())
{
my $error_message = "";
$error_message .= "Please enter your First Name<br>" if (!$firstname );
$error_message .= "Please enter your Last Name<br>" if (!$lastname );
$error_message .= "Please enter your E-mail Address<br>" if (!$email );
if ( $error_message )
{
display_form ( $error_message, $firstname, $lastname, $email);
}
else
{
# Form OK - return success
#return 1;
my $insert_qry = "insert into table(fname, lname, email) values(?,?,?)";
my $sth = $DB->prepare($insert_qry);
$sth->execute($firstname, $lastname, $email);
my $url = "http://www.google.com";
print "Location: $url\n\n";
}
}
sub display_form
{
my $error_message = shift;
my $firstname = shift;
my $lastname = shift;
my $email = shift;
# Display the form
print <<ENDHTML;
<form method="post" action = "reg.pl">
First Name:<input type="text" name=FName size="30">
SurName:<input type="text" name=LName size="30">
E-mail:<input type="text" name=Email size="30">
<br>
<!-- runs the insert query when clicked -->
<button type = "submit" name = insert>
Submit Details
</button>
</form>
ENDHTML
}
when i run this code it doesn't update the database or display error msg. i can't think of any thing else to try
Can anybody help?
thanks in advanced,
sc
You need to declare $firstname, $lastname, and $email as lexical variables with 'my'.
You need to assign them the values posted to the form:
$firstname=$query->param('FName'), etc...
I don't know if <button type=submit>Submit Details</button> will actually work..
Try <input type='submit' value='Submit Details' />
You pull in $error_message, $firstname, $lastname, $email to your display_form sub but never use them.
Good luck