Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Not writing to database

Posted on 2007-12-05
6
193 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

839 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