CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Modifying a flatfile db?

 



xMattHawkx
stranger

May 2, 2001, 2:50 PM

Post #1 of 2 (1318 views)
Modifying a flatfile db? Can't Post

Hi, I have a Pipe-Delimited flat-file database and I'm having quite a bit of trouble with modifying an entry. The problem is, if a person stops the script before it's finished writing the contents of the file back, it deletes the contents of the entire file. Other than that, the script does what it is supposed to do. Here is the code if you want to take a look:

#!/usr/local/bin/perl

use Fcntl;

$profiledb = "cgi-bin/defaultjunk.db";
$itemdb = "cgi-bin/items/defaultjunk.db";

sub get_date {
# Returns the date in the format "dd-mmm-yy".

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(time());
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$day-$months[$mon]-$year";
}

if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$contents{$name} = $value;

}
}

###############################################

print "Content-type: text/html\n\n ";

&no_goi unless $contents{'itemid'};
&no_go unless $contents{'id'};
&no_go unless $contents{'email'};

sub no_go
{
print qq|
<HTML><HEAD><TITLE>Form Incomplete</TITLE></HEAD>
<BODY>
<H1>Form Incomplete</H1>
I'm sorry, the form was not filled completely.

Please click Back and fill it out completely.<p>
Thank you.
</BODY></HTML>
|;
exit;
}

sub no_goi
{
print qq|
<HTML><HEAD><TITLE>Store Error</TITLE></HEAD>
<BODY>
<H1>Store Error</H1>
We are sorry but the owner of the store did not specify a valid Item ID number.

This item cannot be purchased until store owner fixes this problem.<p>
Thank you.
</BODY></HTML>
|;
exit;
}

open(ITEMDB, "<$itemdb");
flock(ITEMDB,LOCK_EX);
while(<ITEMDB> ){
(@resi)=split(/\|/, $_);
if ($resi[0] eq "$contents{'itemid'}"){
$itembig=$resi[2];
$itemmedium=$resi[3];
$itemsmall=$resi[4];
$itemname=$resi[1];
# }else if{
# print "The store owner has entered an invalid Item ID number. Please contact them with the name of the item that you selected. Thank You!";
# exit;
}
}
close(ITEMDB);

open(data, "+<$profiledb");
flock(data,LOCK_EX);
while(<data> ){
(@res)=split(/\|/, $_);
if ($res[0] eq $contents{'id'}){
if ($itembig <= $res[7]) {
if ($itemmedium <= $res[8]) {
if ($itemsmall <= $res[9]) {
$id=$res[0];
$name=$res[1];
$email=$res[2];
$house=$res[3];
$car=$res[4];
$shoes=$res[5];
$job=$res[6];
$newbig=$res[7]-$itembig;
$newmedium=$res[8]-$itemmedium;
$newsmall=$res[9]-$itemsmall;
$classes=$res[10];
$store=$res[12];
$points=$res[13];
$pet=$res[14];
$thedate = &get_date;
if ($res[11]=~/None/i){
$newitems=$itemname;
} else {
$newitems=join ', ' ,$res[11],$itemname;
}
} else {
print "You do not have enough Big, Medium and Small money to buy this item!\n";
exit;
}
} else {
print "You do not have enough Big and Medium money to buy this item!\n";
exit;
}
} else {
print "You do not have enough Big to buy this item!\n";
exit;
}
push(@new, "$id|$name|$email|$house|$car|$shoes|$job|$newbig|$newmedium|$newsmall|$classes|$newitems|$store|$points|$pet|$thedate\n");
}else{
push(@new,$_);
}
}
close(data);

open(new_data, ">$profiledb");
flock(new_data,LOCK_EX);
print new_data @new;
close(new_data);

print "$name, your item, $itemname, has been successfully purchased! Thank You!";
exit;



zanardi
journeyman

May 4, 2001, 2:05 PM

Post #2 of 2 (1301 views)
Re: Modifying a flatfile db? [In reply to] Can't Post

Well, I had a problem reading that last open function, to many If's inside of each other that didn't need to be (I dont think. agian, I dont know how your script is exactly supposed to work...)

another thing, you should use the cgi.pm to parse your forms. And if you spend enough time with it, you'll find that it does alot of time saving things.

anyway, see if replacing that open function with this does anything with your script:


Code
$thedate = get_date(); # why put this in the loop and call it everytime... talk about bad 

open(data,$profiledb) or die "cant find profile file! $!";
while(<data>) {
($id,$name,$email,$house,$car,$shoes,$job,
$newbig,$newmedium,$newsmall,$classes,$store,$points,$pet) = split(/\|/,$_);

$newbig = $newbig - $itembig;
$newmedium = $newmedium - $itemmedium;
$newsmall = $newsmall - $itemsmall;

if ($store =~ /none/i){
$newitems = $itemname;
} else {
$newitems = join ', ' ,$res[11],$itemname;
}

unless ($itembig <= $res[7]) {
print "You do not have enough Big to buy this item!\n";
exit;
}

unless ($itemmedium <= $res[8]) {
print "You do not have enough Big and Medium money to buy this item!\n";
exit;
}

unless ($itemmedium <= $res[8]) {
print "You do not have enough Big and Medium money to buy this item!\n";
exit;
}

unless ($itemsmall <= $res[9]) {
print "You do not have enough Big, Medium and Small money to buy this item!\n";
exit;
}

if ($res[0] eq $contents{'id'}) {
push(@new,"$id|$name|$email|$house|$car|$shoes|$job|$newbig|$newmedium|$newsmall|$classes|$newitems|$store|$points|$pet|$thedate\n");
} else {
push(@new,$_);
}
}
close(data);

----------------------
Fueled By ZCom

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives