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? }

In reply to Re: Syntax Help by voyager
in thread Syntax Help by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.