Solved

Not writing to database

Posted on 2007-12-05
6
191 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 …
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

22 Experts available now in Live!

Get 1:1 Help Now