Query that changes the amount of items in Mysql

Asked

Viewed 44 times

-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

Then I use the query, at first it works normal (as I said upstairs), but then Buga, I put to ID:10 increase 5, then it goes to 15 (normal), but I put to ID:11 increase 10, instead of going to 30 it 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>");
}   



?>

1 answer

1


To query SELECT * FROM lentes returns all registered lenses. There should be no clause WHERE for the specific lens ($idad)?

  • Wow, I can’t believe it was such a beast kk, I didn’t think it was that just because some of them worked normal, thanks a lot :D

Browser other questions tagged

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