-1

there is table named Book and this code is supposed to find a specific record and edit it's num to num-1, but it only edit num to -1.

$sql = 'SELECT num FROM Book
    WHERE bid="'.$_SESSION["sails bid{$i}"].'"';
if (mysqli_query($conn, $sql)) {
    $row = mysqli_fetch_array($query);
    $numbers=(int)$row['num']-1;
    $sql='UPDATE Book
    SET num="'.$numbers.'"
    WHERE bid="'.$_SESSION["sails bid{$i}"].'"';
    if (mysqli_query($conn, $sql)) {
        ...
    }
}
Leyla Krz
  • 96
  • 1
  • 10

1 Answers1

2

For one thing, you've overcomplicating this. You don't need to select into PHP, perform the math, and then update. Simple arithmetic can be performed in SQL. For example:

UPDATE Book SET num = num - 1 WHERE bid = ?

Just bind your bid value (preferably using query parameters instead of the string concatenation you're currently using) and you only need to execute that one UPDATE instead of all the code you have now.

David
  • 208,112
  • 36
  • 198
  • 279