Click to See Complete Forum and Search --> : issues with a subroutine, keeps telling me thre too many arguments


Geowil
12-07-2008, 01:03 PM
I gave up on getting POST and php to work but I was able to get it working with a cgi script.

So now I am implementing my sql connection subroutine and am passing the POSTed variables into it. The only problem is the subroutine wont take all the arguments for whatever reason.

here is the call:


connect($fName,$fName2,$email);


and here is the sub definition:
[code]
sub connect {

my($firstname, $lastname, $email) = @_;






$link = "DBI:mysql:database_name:localhost";

my $link2 = DBI->connect($link, "username", "password") || die "Can't Connect";



## insert fill-in form input into database:



my $sel = $link2->prepare("INSERT INTO 'testusers' VALUES($firstname,$lastname, $email)");



$sel->bind_param(1, $firstname);

$sel->bind_param(2, $lastname);

$sel->bind_param(3, $email);

$sel->execute() && $sel->finish();

$result = mysql_query($sel);

if ($result) {
print header;
print "Account created successfully!";
print end_html;
}

else {
print header;
print "Creation Failed for following reason: ";
print(mysql_error());
print end_html;
}




$dbh->disconnect;
}

here is the error out put from that page:


Software error:

Too many arguments for connect at testcgi.cgi line 17, near "$email)"
Execution of testcgi.cgi aborted due to compilation errors.


why is it spitting that out? Are subs limited to only 2 arguments or is something else wrong with the script that I dont see?.

chazzy
12-07-2008, 03:29 PM
maybe you could give us a bit more code, how it grabs $fName etc.

Geowil
12-07-2008, 05:13 PM
here is the entire page:


#!/usr/bin/perl -wT
use DBI;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);


my $fName = param('firstname');
my $fName2 = param('lastname');
my $email = param('email');
print header;
print ("Hello ");
print ($fName);
print ($fName2);
print ($email);
print end_html;

&connect($fName, $fName2, $email);

print header;
print "Account made successfully";
print end_html;




sub connect {

my($firstname, $lastname, $email) = @_;






$link = "DBI:mysql:xilohen_XiloGallery:localhost";

my $link2 = DBI->connect($link, "", "") || die "Can't Connect";



## insert fill-in form input into database:



my $sel = $link2->prepare("INSERT INTO 'testusers' VALUES($firstname,$lastname, $email)");



$sel->bind_param(1, $firstname);

$sel->bind_param(2, $lastname);

$sel->bind_param(3, $email);

$sel->execute() && $sel->finish();


$link2->disconnect;
}

chazzy
12-07-2008, 05:30 PM
could you clarify...
are you calling it via


&connect($fName, $fName2, $email);

or

connect($fName, $fName2, $email);


either way, now that i'm thinking about it it's really because at the top of your script you have


my $email = param('email');
which makes $email available to everything in your script. but later on you do this...

my($firstname, $lastname, $email) which I imagine perl is considering a conflict. you should in fact remove $email from this listing, or else rename the local sub's variable name to something else.

Geowil
12-07-2008, 09:33 PM
ah, cool its working now (took out the subroutine section you mentioned).

Thanks!

another quick question, why is this


Content-Type: text/html; charset=ISO-8859-1

getting outputted after all my print end_html's?

chazzy
12-08-2008, 07:11 AM
ah, cool its working now (took out the subroutine section you mentioned).

Thanks!

another quick question, why is this


getting outputted after all my print end_html's?

Hmm i'm not too familiar with it. Is it something that you defined or perl gives you? looks like you may be calling it in a weird way - since you've got header and end_html twice, each. I think there should only be one

(sorry for the vague answer, i'm a perl server clean up type of programmer, with my client/server stuff being in java)

PolyGreat
12-08-2008, 08:53 AM
Indeed...chazzy is on the right track. It is the header that prints the "Content-header..." line. Therefore, this

my $fName = param('firstname');
my $fName2 = param('lastname');
my $email = param('email');
print header;
print ("Hello ");
print ($fName);
print ($fName2);
print ($email);
print end_html;

&connect($fName, $fName2, $email);

print header;
print "Account made successfully";
print end_html;


should be this

my $fName = param('firstname');
my $fName2 = param('lastname');
my $email = param('email');
print header;
print ("Hello ");
print ($fName);
print ($fName2);
print ($email);

&connect($fName, $fName2, $email);

print "Account made successfully";
print end_html;


Blessings,

PolyGreat

Geowil
12-08-2008, 11:19 AM
ah, i see. cool, thanks again.