SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot revivalx's Avatar
    Join Date
    Dec 2009
    Location
    Kuala Lumpur,Malaysia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calculation with php and mysql

    Hi

    I had a problem about to calculate between php and mysql. How I can deduct a value from database in mysql , write a code to integrate php and mysql. I had a form that contain a value that will be deduct from database.

    value from database - value from form(php)=$result

    the $result will be store in a same table in database.

    The conclusion is, how I create a code for calculation?
    tq


    Web advertising solution

    Business is not about money, it is about trust..

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As with most things, there are at least two ways to tackle this problem:

    You could retrieve the current value from the database into a PHP variable using a SELECT query, then subtract the value submitted with the form from that PHP variable, and then perform an INSERT or UPDATE query to store the resulting value back into the database.

    Alternatively, depending on the database structures involved and the version of MySQL you are running, you may be able to do the whole job using an SQL query. An INSERT or UPDATE query could include a SELECT query that retrieved the existing database value and subtracted the value submitted through the form in order to calculate the new value to be stored in the database. This is a fairly advanced SQL technique, however, and I'd need to know more about your database structure to advise you on what the code would look like.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Before writing the code in assumption, can you show us your table structure of source table from where you will get to subtract and target table where you want to store the result so that we can give some codes for you?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can't you just

    "UPDATE table SET value = value - ".$form_val." WHERE id=1"

    Edit, ah, two tables??

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it is in the same table then it can be something like this i guess:
    PHP Code:
    $sql "UPDATE table SET result = value - " mysql_real_escape_string($form_val) . " WHERE id=1"
    I just assumed value field is the value from which the form value is to be subtracted and result is the field where you want to store the result.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Zealot revivalx's Avatar
    Join Date
    Dec 2009
    Location
    Kuala Lumpur,Malaysia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    //table stock

    CREATE TABLE 'stock'(
    'stock_id' int(5) NOT NULL auto_increment,
    'quantity' int(3) default NULL,
    PRIMARY KEY('stock_id')
    )

    //table take

    CREATE TABLE 'take'(
    'take_id' int(5) NOT NULL auto_increment,
    'quantity' int(3) default NULL,
    PRIMARY KEY('take_id')
    )

    //form in insert.php
    <form action="" method="post" name="insert">
    <table>
    <tr>
    <td>Quantity</td>
    <td><label>
    <input name="quantity" type="text" class="text" id="quantity" size="3" maxlength="3" />
    </label></td>
    </tr>
    <tr>
    <td><label>
    <input type="submit" name="Submit" id="button" value="Submit" />
    </label></td>
    </tr>
    </table>
    </form>

    how to create substraction between php and mysql

    this is a formula for subtraction

    $result=quantity(from table stock)- quantity(from table take)


    Web advertising solution

    Business is not about money, it is about trust..

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how to create substraction between php and mysql
    this is a formula for subtraction
    $result=quantity(from table stock)- quantity(from table take)
    You want to subtract quantity of take table with quantity of stock table? Then what is the use of form quantity?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Zealot revivalx's Avatar
    Join Date
    Dec 2009
    Location
    Kuala Lumpur,Malaysia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    form quantity is when I insert the quantity value . that value will be deduct with quantity value in table stock.

    quantity value from table stock-quantity value from form quantity


    Web advertising solution

    Business is not about money, it is about trust..

  9. #9
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sql = "UPDATE table SET result = value - " . mysql_real_escape_string($form_val) . " WHERE id=1";
    that's wrong approach.
    mysql_real_escape_string means nothing without quotes.
    I'd use intval() instead for this case.

    revivalx.
    In general you do create an SQL query in PHP. That's the way PHP interacted with mysql. So, as everyone told you, you have to create an SQL query in PHP. And query logic is up to you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •