How to make a condition with mysql number php

1

Hi I'm doing a virtual bank type in a part has to make the deposit, the user has the money in the portfolio for example.

I have 500 reais in my wallet

But if I put any value above 500 it will deposit in the bank account and I wanted the user to only put the maximum amount that he has in his wallet

Code to send value

<center>
    <select name="id">

<?php
    $result_usuario = "SELECT * FROM 'usuarios' WHERE id=1";
    $mostra_dados = mysqli_query($conn, $result_usuario);
    while($rows_cursos = mysqli_fetch_assoc($mostra_dados)){ ?>
   <option value="<?php echo $rows_cursos['id']; ?><?php echo $rows_cursos['usuario']; ?></option>
 <?php } ?>
 </select>
    
asked by anonymous 23.07.2018 / 02:13

1 answer

1

I added the max field to <input> that it types the value with the value that it has with account (you change the name of the fields that are needed), which will limit to the amount that it takes into account, in the update you do a reconfirmation if the values are in agreement before running the update.

<?php
    $result_usuario = "SELECT * FROM 'usuarios' WHERE id=1";
    $mostra_dados = mysqli_query($conn, $result_usuario);
    while($rows_cursos = mysqli_fetch_assoc($mostra_dados)){ 
        $dinheiroEmConta=$rows_cursos['dinheiro'];
?>
        <option value="<?php echo $rows_cursos['id']; ?>"><?php echo $rows_cursos['usuario']; ?></option>
    </select>
        <input type="text" name="valor" max="<?php echo $dinheiroEmConta; ?>">
 <?php 
    }
?>

This excerpt should come from the part where SELECT that generates $result_usuario occurs, so you can create the $dinheiroEmConta variable that I added in the above code block, and then do this:

if((isset($_POST['dinheiro']))&&(!empty($_POST['dinheiro']))){
    $id = $_POST['id'];
    $dinheiro = $_POST['dinheiro'];
    if($dinheiro <= $dinheiroEmConta){

        $_SESSION['id'] = $id; 
        $_SESSION['dinheiro'] = $dinheiro;

        $recebe_dados = "UPDATE usuarios SET dinheiro = dinheiro -'$dinheiro', depositado = depositado +'$dinheiro' WHERE id = '". $_SESSION['usuarioId'] ."'"; 
        $result_usuario = "UPDATE usuarios SET dinheiro = dinheiro +'$dinheiro' WHERE id = $id"; 
    }
}
    
23.07.2018 / 02:29