Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Not writing to database

Posted on 2007-12-05
6
Medium Priority
?
198 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
[X]
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
  • 3
  • 2
6 Comments
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1600 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

618 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