Query that changes the amount of items in MySql [closed]

-1

I have a query that changes the amount in MySQL (below). Initially this query works perfectly, but when I register new items in the table, some of them do not change the quantity correctly, but some remain normal.

For example, in this query I update from the lens ID (which is the product I use in this system):

ID:10 Quantidade: 10
ID:11 Quantidade: 20

So I use the query , in the beginning it works normal (like I said up there), but then buga , I put pro ID: 10 increase 5, he goes to 15 (normal), but I put pro ID: 11 increase 10, instead of going to 30 he goes to 23, or 12, or any other number.

<?php
include_once("db.php");
$idad = $_POST['idad'];
$qntad = $_POST['qntad'];

$sql = "SELECT * FROM lentes ";
$query = $con->query($sql);
while($lentes = $query->fetch_array()){
    $idlente = $lentes["ID"];
    $qntlente = $lentes["quantidade"];
}
$altera = ($qntlente) + ($qntad);

$sql="UPDATE 'lentes' SET 'quantidade' = $altera WHERE 'lentes'.'ID' = $idad";


   if (mysqli_query($con,$sql))
    print "<script>
    alert('Lente de ID $idad atualizada com sucesso! Saldo atual: $altera')
location.href='index.php'
</script>";
     {
    die(print "<script>
    alert('Ops! Lente fora de estoque! Saldo atual: $qntlente')
location.href='index.php'
</script>");
}   



?>
    
asked by anonymous 26.06.2018 / 20:55

1 answer

1

The query SELECT * FROM lentes returns all registered lenses. Should not there be a WHERE clause for the specific lens ( $idad )?

    
26.06.2018 / 21:03