I want to take a current balance and add up to a previous balance

-1

This system and to do banking operations the user will have to make the deposit only he type a value that he wanted to deposit and in the bank he will have to add to the current balance only that is not working. he is putting a number 7 and is not doing the sum

<?php
$idConta=$_POST['idConta'];
$valor=$_POST['valor'];

$consu= mysql_query("select saldo from conta where  idConta=$idConta");

$saldoatual = $consu + $valor;


$up = mysql_query("UPDATE conta SET saldo='$saldoatual'  WHERE idConta=$idConta");



?>

    
asked by anonymous 08.06.2018 / 17:16

1 answer

1

Your code is wrong mysql_query does not return data, it returns the query handler executed, so you should use mysql_fetch_assoc to get the values, for example:

  

ps: add isset to check if the variables came via POST correctly

if (isset($_POST['idConta']{0}, $_POST['valor']{1})) {

    $idConta=$_POST['idConta'];
    $valor=$_POST['valor'];
    $up = false;

    $consu = mysql_query("select saldo from conta where  idConta=$idConta");

    if ($consu) {
        $dados = mysql_fetch_assoc($consu);

        $saldoatual = $dados['saldo'] + $valor;

        $up = mysql_query("UPDATE conta SET saldo='$saldoatual'  WHERE idConta=$idConta");
    } else {
         echo 'Erro:', mysql_error();
    }
}

Extra

However, I recommend to update and stop using this API (functions that start with the prefix mysql_ ) that is already so obsolete, to access your database mysql prefer the new APIs as:

  • PDO
  • mySqli
08.06.2018 / 17:44