0

The success statement shows after i input the quantity i want to add but my update statement doesn't seem to be updating my database and but I have no idea why, , I've used the same code elsewhere in my code and it works fine.

<?php
    include 'includes/session.php';

    if(isset($_POST['edit'])){
        $id = $_POST['id'];
        $name = $_POST['name'];
        $quantity = $_POST['quantity'];

        try{
            $stmt = $conn->prepare("UPDATE stock_list SET quantity=:quantity+'$quantity' WHERE id=:id");
            $stmt->execute(['quantity'=>$quantity, 'id'=>$id]);
            $_SESSION['success'] = 'Stock updated successfully';
        }
        catch(PDOException $e){
            $_SESSION['error'] = $e->getMessage();
        }
        
        $pdo->close();
    }
    else{
        $_SESSION['error'] = 'Fill up edit Stock form first';
    }

    header('location: stockin.php');

?>
Sencho
  • 13
  • 2

1 Answers1

0

If you want to increase the value of a data field (e.g. xxx) by (yyy) , it is:

update [tablename] set xxxx=xxx + :yyy where [conditions]

So change this block

try{
            $stmt = $conn->prepare("UPDATE stock_list SET quantity=:quantity+'$quantity' WHERE id=:id");
            $stmt->execute(['quantity'=>$quantity, 'id'=>$id]);
            $_SESSION['success'] = 'Stock updated successfully';
        }

to

try{
            $stmt = $conn->prepare("UPDATE stock_list SET quantity=quantity+:quantity WHERE id=:id");
            $stmt->execute([':quantity'=>$quantity, ':id'=>$id]);
            $_SESSION['success'] = 'Stock updated successfully';
        }
Ken Lee
  • 6,985
  • 3
  • 10
  • 29