sum of PHP and MySql values

1

Good afternoon, Friends I am creating here a system of notes to use in the company he is very basic, but I am in doubt on how to set up the payment function so that it identifies the values goes down the amount paid and create a balance. my table and as follows

id | cliente | valor | pago | Deb/Cred
1  | teste   | 35.00 | N    | D
2  | teste   | 35.00 | N    | D
3  | teste   | 35.00 | N    | D
4  | pago    | 65.00 | S    | C
5  | saldo   | 40.00 | N    | C

I would like to know how to mount the formula in php so that when I launch the payment it adds the values to the id 3 that are only debits that give a value of 105.00 subtract the payment that is in the id 4 that is a credit taking the result that is 105.00-65.00 = 40.00 to launch in id 5 as credit and substitute in the column payment N for S to identify that this payment

    
asked by anonymous 01.06.2016 / 19:19

1 answer

1

Personnel I was able to solve with the following SELECT

SELECT
(SELECT SUM(IF(debito_credito_financeiro = 'C', valor_financeiro, -valor_financeiro)) FROM cad_financeiro WHERE cad_financeiro.pago_financeiro = 'N') AS saldo
FROM cad_financeiro
JOIN cad_trabalho ON cad_financeiro.id_trabalho = cad_trabalho.id_trabalho
JOIN cad_paciente ON cad_trabalho.id_paciente = cad_paciente.id_paciente
JOIN cad_cliente ON cad_paciente.id_cliente = cad_cliente.id_cliente
WHERE cad_cliente.id_cliente = '$id'
GROUP BY cad_cliente.id_cliente

and creating a function in php to give an UPDATE in bd

    
02.06.2016 / 21:46