Solved

Not writing to database

Posted on 2007-12-05
6
190 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
Comment Utility
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
Comment Utility
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
Comment Utility
Mysql,
INSERT INTO bookaccounts (acc_num, `password`) ...
etc
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
@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
Comment Utility
       my $query = $dbh -> prepare("INSERT INTO bookaccounts (ID, acc_num, password) VALUES (null, '$acc_num', '$password')");
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
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 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…
The viewer will learn how to dynamically set the form action using jQuery.

763 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

10 Experts available now in Live!

Get 1:1 Help Now