1

I want to add the rows that is being query in database. There is an amount paid, previous balance, new balance.. I want the previous balance to be added by paid amount.

Num|pay|old|new

1  |100|500|600

2  |120|600|720

3  |200|720|920

4  |300|720|920

5  |350|720|920

6  |500|720|920

The database query has data of amountPaid (pay), previous balance (old), new balance (new), but the data in column(new) is wrong.I want to correct it in forloop.Is there a way to help me in this? to get the column(new) value is column(pay)100 + column(old)500 = column(new)600. the column(new) in number two will be put in column(old)600 then added by column(pay)120 is equal to column(new)720 and so on.

Hope you can help me in this...

1
  • update table_name set new = pay + old Commented Sep 7, 2014 at 7:43

1 Answer 1

1

I suppose you are adding the amounts when user submits it

1) Use mysql UPDATE clause in order to change the value in the coloumns

2) use while loop to get the previous value selecting it with a specific id which I suppose you have in your table

3) add it with a new value and update the new amount coloumn

PHP

<?php
     $id=$_POST['id'];
     $amount=$_POST['amount'];

    include("connection.php"); 
    $sql="SELECT * FROM  `tablename` WHERE  `user_id` ='$id'";
    $res=mysql_query($sql);
    $row=mysql_fetch_array($res){


            $t2 = $row['previous_amount'];
            $t3=$t2+$amount;
            $sql="UPDATE  `bank`.`tablename` SET `new_amount' = '$t3'";
        }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes but some data is already in the database and i want to fix it. is there a way to update all data in the database. The amount has specific id and person. i want the first payment which can be determined in the datepaid to be the start of update then next is so on.
" i want the first payment which can be determined in the datepaid to be the start of update then next is so on" what do you mean by that ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.