Forum Moderators: coopster & phranque

Message Too Old, No Replies

Saving and removing data from a database

         

Newbie77

1:24 am on Sep 7, 2004 (gmt 0)

10+ Year Member



I am not sure where I am going wrong with my code. I need to be able to enter an order or delete one using three boxes; price, code and name and have the information saved to a database. I also need to remove any leading and trailing spaces and dollar signs and save the product code using capitol letters. Also where do the .dir and .pag files fit in, I know i need them to finish this but I am lost? Here is my code so far.

#declare variables
my ($button, $code, $name, $price);

#assign values to variables
$button = param('Button');
$code = param('Code');
$name = param('Name');
$price = param('Price');
if ($button eq "Save") {
add();
}
elsif ($button eq "Delete") {
remove();
}
exit;

#*****user-defined functions*****

sub add {
#declare variables
my %order;

#open database, format and add record, close database
tie(%order, "SDBM_File", "orderlist", O_CREAT¦O_RDWR, 0666)
or die "Error opening orderlist. $!, stopped";
$order{$code} = "$name,$price";
untie(%order);

#create web page
print "<HTML>\n";
print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
print "<BODY>\n";
print "<FONT SIZE=4>Thank you for ordering $name, $code, $price.<BR>\n";
print "</BODY></HTML>\n";
} #end add

sub remove {
#declare variables
my (%order, $msg, %dummy);

#open database
tie(%order, "SDBM_File", "orderlist", O_RDWR, 0)
or die "Error opening orderlist. $!, stopped";
#determine if the product exists
%dummy = %order
$dummy{$code}= "$name, $price";
if (exists($dummy{$code})) {
delete($dummy{$code});
$msg = "Thank you, $name has been removed from your order.";
}
else {
$msg = "There is an error in your order please go back";
}
#close database
untie(%order);

#create web page
print "<HTML>\n";
print "<HEAD><TITLE>Candles Unlimited</TITLE></HEAD>\n";
print "<BODY>\n";
print "$msg\n";
print "</BODY></HTML>\n";
} #end remove

cyberws

10:28 pm on Sep 13, 2004 (gmt 0)

10+ Year Member



Remove inital trailing space:

$variable=~ s/^\s+//; #remove initial space

Remove trailing slash:

$a =~ s/\s+$//;

Remove dollar sign:

$variable=~ s/\$//g;

Upper case all in variable:

$variable=uc("$variable");