Oringinal node:
I need help doing some basic arithmatic on some database values. Initi
+ally, I query my table for all product codes and quantity ( using DBI
+ ). Then I display these non-editable values along with two editable
+fields for entering a value that will be added/subtracted from origin
+al quantity in a form (using CGI) so it looks something like this:
Product Name | Quantity | Add | Subtract
WidgetA | 10 | |
.
.
.
What I need to do happen when the form is submitted is take all the in
+put in the add and/or subtract fields and do the appropriate math on
+the original quantity value, then store the newly calculated quantity
+ back into the table. Can anyone please give me a good example to wor
+k with?
Name each of your fields "add_keyvalue" and "sub_keyvalue", where the "keyvalue" part is the key to the row in the database (maybe your product_name?).
Then in your script:
use CGI;
my $q = CGI->new;
foreach my $parm ($q->param) {
if ($parm =~ m/^add_(.*)$/) {
sub adjust_qty($1, $q->param($parm));
}
if ($parm =~ m/^sub_(.*)$/) {
sub adjust_qty($1, -($q->param($parm)));
}
}
sub adjust_qty {
my ($key, $qty) = @_;
my $sql = qq(update table set qty = qty + $qty where key = $key);
# do you need DBI help here?
}
|