Solved

Not writing to database

Posted on 2007-12-05
6
192 Views
Last Modified: 2013-12-25
I have a script to help play around with simple simple encrypt using tr with Perl and I cant get it  to write to my database.

Any suggestions? I have been on this for awhile..my connections are correct, my variables match up.

Any thoughts?

Thanks,
Ryan
#!/usr/bin/perl
use CGI qw(:standard);
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) or die print "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 (acc_num, password) VALUES ('$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
  • 2
6 Comments
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 100 total points
ID: 20416507
First thing to do is redirect the fatal errors to the browser, and enable RaiseError on the db calls.

use CGI::Carp qw(fatalsToBrowser);

....
....
....
....

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

Expert Comment

by:imitchie
ID: 20416509
What dbms are you using?
Some of them do not like "password" for a column name.
For SQL Server for example, use

INSERT INTO bookaccounts (acc_num, [password]) ...
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20416514
Mysql,
INSERT INTO bookaccounts (acc_num, `password`) ...
etc
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20416545
@Fish:

Thanks man! What exactly does this do:
 >>{RaiseError => 1}

So the - use CGI::Carp qw(fatalsToBrowser); - makes it show the error? Correct?

Just trying to get an understanding of Perl more. I forgot to mention i have the field name ID that is auto_increment. Where do i specify that in my query to the database?

Because that is causing this error:

DBD::mysql::st execute failed: Duplicate entry '0' for key 1 at booksonline.cgi line 30.

It just keeps writing the same ID.


#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
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 (acc_num, password) VALUES ('$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
 
LVL 25

Accepted Solution

by:
imitchie earned 400 total points
ID: 20416574
       my $query = $dbh -> prepare("INSERT INTO bookaccounts (ID, acc_num, password) VALUES (null, '$acc_num', '$password')");
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20416591
Thank you both for your help! Appreciate it! Next I am trying to distingush the difference between both of these submit buttons:

http://elan.champlain.edu/~rcoughlin32001/booksonline.html

Create an if? I know how to in PHP user REQUEST==NAME etc

I will post another question..thank you both!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

810 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