Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Add/subtract from a database value

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

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 LeGo (Chaplain) on Jul 06, 2001 at 04: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 Abigail (Deacon) on Jul 06, 2001 at 03: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

A reply falls below the community's threshold of quality. You may see it by logging in.

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
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2025-03-20 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (60 votes). Check out past polls.