Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Do you know where your variables are?
 
PerlMonks  

Add/subtract from a database value

by Anonymous Monk
on Jul 05, 2001 at 23:07 UTC ( [id://94335]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need help doing some basic arithmatic on some database values. Initially, 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 original 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 input 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 work with?

Replies are listed 'Best First'.
Re: Add/subtract from a database value
by Abigail (Deacon) on Jul 05, 2001 at 23:18 UTC
    Eh, what piece do you have problems with? Everything seems trivial to me. Use an SQL statement to retrieve the values from the table. Use print to print out the form. Use CGI to parse the form response. Use + and - to add or subtract values, and use another SQL query to store the results.

    -- Abigail

Re: Add/subtract from a database value
by LeGo (Chaplain) on Jul 06, 2001 at 00:26 UTC
    Would something like this help? Just use those commands and adapt.
    $dbh = DBI->connect( "dbi:mysql:user", "webserver", "pwd") or dienice( +"Can't connect: ",$dbh->errstr); my $sub = param("sub"); my $add = param("add"); if($add){ $sth = $dbh->do("update database set count=count+$add where address +=\"$whatever\"") } if($sub){ $sth = $dbh->do("update database set count=count-$sub where address +=\"$whatever\"") }

    Feel free to update this or correct this as this is the way I do things and if there is a better way or something that I am missing please do let me know.

    LeGo

Re: Add/subtract from a database value
by Anonymous Monk on Jul 06, 2001 at 06:28 UTC
    If Im naming my "Add" and "Sub" fields like this:
    while ( my $fieldnames = $db->fetchrow_hashref ) { #append product code to "add_" so all text fields #are named "add_WidgetA" and so forth }
    how do I go about getting every value and then adding to the appropriate field?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://94335]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.