Solved

Two actions - Two submit buttons

Posted on 2007-12-05
5
203 Views
Last Modified: 2011-10-03
I have this page - http://elan.champlain.edu/~rcoughlin32001/booksonline.html - and submits the data to a database.

The first button submits the page to the database
The second button i want to be able to retrieve the password based on the account number given.

I am confused on how to make each a different action. I have them as two different names..but am not sure how to call them in Perl. In PHP I know you can do a REQUEST say and have it equal a name.

I am thinking a simple IF ELSE

IF CREATE

   CREATE ACCOUNT

ELSE RETRIEVE

  RETRIEVE PASSWORD
  SQL SELECT STATEMENT FROM DB

Any ideas?

Thanks,

Ryan


#!/usr/bin/perl

use CGI qw(:standard);

use CGI::Carp qw(fatalsToBrowser); # errors will fall to browser and be printed

use DBI;

use strict;
 

print "Content-type:text/html \n\n";
 

# username and password for the user

my $acc_num   = param('acc_num');

my $password  = param('password');
 

# login information

my $database  = "rcoughlin32001";

my $dbuser    = "rcoughlin32001";

my $dbpass    = "lacrosse";
 

# select, type, database, server

my $dsn       = "DBI:mysql:$database:localhost";
 

# connect

my $dbh       = DBI -> connect($dsn, $dbuser, $dbpass, {RaiseError => 1}) or die "Error connecting: $DBI::errstr\n";
 

	# encrypt then store in database

	# replace a-k with 0-9

	$password  =~ tr/[a-k]/[0-9]/;

	

	# store data in database

	my $query = $dbh -> prepare("INSERT INTO bookaccounts (ID, acc_num, password) VALUES (null, '$acc_num', '$password')");

	my $done  = $query -> execute();

	

	if($done){

		print "Added to database.<br />";

	}else{

		print "Didn't add to database. <a href=\"../booksonline.html\">Click</a> to go back.<br /><br />";

	}

	# for debug

	print "<b>OUTPUT</b><br /><br />";

	print "<b>Account Number:</b> " . $acc_num . "<br/>";

	print "<b>Password:</b> " . $password;

Open in new window

0
Comment
  • 3
5 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 400 total points
ID: 20416647
You could leave your form as is, but I find it easier to use the same submit name, eg:

<input type="submit" name="action" value="Create Account" /><label></label>
<input type="submit" name="action" value="Retrieve Password" />

Then in the Perl script, you do

my $action = param('action');

if ($action eq 'Create Account') {
  ...
}
elsif ($action eq 'Retrieve Password') {
..
}
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20416700
Nice..thank you! Easier than i thought it would be!

I get this:

>>Can't call method "execute" on an undefined value at booksonline.cgi line 54.

Line 54:       my $sth-> execute;

Any ideas? And to decrypt..does that look right..what i have in my code?

Thanks!
#!/usr/bin/perl

use CGI qw(:standard);

use CGI::Carp qw(fatalsToBrowser); # errors will fall to browser and be printed

use DBI;

use strict;
 

print "Content-type:text/html \n\n";
 

# username and password for the user

my $acc_num   = param('acc_num');

my $password  = param('password');

my $action    = param('action');
 

# login information

my $database  = "rcoughlin32001";

my $dbuser    = "rcoughlin32001";

my $dbpass    = "lacrosse";
 

# select, type, database, server

my $dsn       = "DBI:mysql:$database:localhost";
 

# connect

my $dbh       = DBI -> connect($dsn, $dbuser, $dbpass, {RaiseError => 1}) or die "Error connecting: $DBI::errstr\n";
 

# if create account was pushed

if($action eq 'Create Account'){
 

	# encrypt then store in database

	# replace a-k with 0-9

	my $encrypt_pass  =~ tr/[a-k]/[0-9]/;

	

	# store data in database

	my $query_create = $dbh -> prepare("INSERT INTO bookaccounts (ID, acc_num, password) VALUES (null, '$acc_num', '$password')");

	my $done  = $query_create -> execute();

	

	if($done){

		print "Added to database.<br />";

	}else{

		print "Didn't add to database. <a href=\"../booksonline.html\">Click</a> to go back.<br /><br />";

	}

	# for debug

	print "<b>OUTPUT</b><br /><br />";

	print "<b>Account Number:</b> " . $acc_num . "<br/>";

	print "<b>Password:</b> " . $password;

}elsif($action eq 'Retrieve Password'){

	

	# decrypt then print

	# replace a-k with 0-9

	my $decrypt_pass  =~ tr/[0-9]/[a-k]/;

	

	# store data in database

	my $query_retrieve="SELECT password FROM bookacounts WHERE acc_num=\"$acc_num\"";

	my $sth = $dbh->prepare($query_retrieve);

	my $sth-> execute;

	

	print $sth;

}

Open in new window

0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 100 total points
ID: 20416708
Change
my $sth-> execute;

To
$sth-> execute;
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20416769
Thank you both!
Ryan
0
 
LVL 1

Author Closing Comment

by:catonthecouchproductions
ID: 31413030
Worked like a charm!

Thanks again,

Ryan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now