IF condition with database

Asked

Viewed 337 times

1

Guys I’m making a money transfer system via php with database, I have an option showing all database users and a text field to enter a value, how I do so that the user can not select himself and increase his money

Field code:

$result_usuario = "SELECT usuario FROM `usuarios`";
$mostra_dados = mysqli_query($conn, $result_usuario);
while($rows_cursos = mysqli_fetch_assoc($mostra_dados)){
<option name="id" value="<?php echo $rows_cursos['id'];?>"><?php echo $rows_cursos['usuario'];?></option>

Code to update and check:

if ($id['id'] == $rows1['id']){
    $_SESSION['id'] = $id;
    echo "<br><div class='alert alert-success'>Impossivel enviar dinheiro para voce mesmo ESPERTALHÂO</div>";
} elseif ($dinheiro > $rows['dinheiro']){
    $_SESSION['dinheiro'] = $dinheiro;
    echo "<br><div class='alert alert-success'> Sem Dinheiro Suficiente seu saldo de dinheiro em carteira é de ". $rows2['dinheiro'] ." Reais </div>";
} else {
    $recebe_dados1 = "UPDATE usuarios SET dinheiro = dinheiro -'$dinheiro' WHERE id = '".$_SESSION['usuarioId'] ."'"; 
    $recebe_dados2 = "UPDATE usuarios SET dinheiro = dinheiro +'$dinheiro' WHERE id = '".$rows1['id'] ."'"; 
    $recebe_dados4 = "UPDATE usuarios SET total = dinheiro + depositado WHERE id = '". $_SESSION['usuarioId'] ."'";
    echo "<script>window.location='?pagina=logado';alert('Você fez uma transferencia de $dinheiro Reais.');</script>";
  • Have login? Vc stores some user identifier?

1 answer

2


Your code has an inconsistency. Maybe more... But I will quote one that is very clear to me.

In this select you select only the user field and then you want to try using the id. So this won’t work. This would be right:

"SELECT id,usuario FROM `usuarios`"

To select all users except the one that is logged in, you can do this directly in the bank:

$result_usuario = "SELECT id,usuario FROM `usuarios` WHERE NOT id = ".$_SESSION['id'];

This way the smart-ass won’t be able to select himself.

Of course, the moment you go to make the transfer is good to see if he has not found a way to release his own id. This if I would do something like that:

if ($_SESSION['id'] != $_POST['id'] && $dinheiro >= $_POST['dinheiro']){
    // transferindo...
    // update...
}
  • 1

    Thanks I managed to do only with that same $result_usuario = "SELECT id,user FROM usuarios WHERE NOT id = ". $_SESSION['id'];

  • @Cool mateussilva! But beware... The foretold man died of old.

  • 1

    kkkkk, I like the espertalhão, well applied term, even more so than WHERE NOT will my +1

  • kkk valeu @Leocaracciolo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.