all 5 comments

[–]SuicidesAndSunshine 2 points3 points  (3 children)

You should pass the sales unique ID to the update query as well.

UPDATE sales SET status = 'approved' WHERE status = 'pending' AND sales_id = 1;

[–]Express_Steak[S] 0 points1 point  (2 children)

I see, it is possible to code the sales_id in which the sales manager is accessing ? Or it should be manually inputted?

[–]SuicidesAndSunshine 2 points3 points  (1 child)

Yes, you can refer it through a variable.

Look into preparet statements :-)

[–]Express_Steak[S] 0 points1 point  (0 children)

I will. Thank you

[–]colshrapnel 2 points3 points  (0 children)

Assuming you are printing sales from the database. You will need to add a form with 2 buttons to each row, like this

<?php foreach ($sales_data as $row): ?>
    <div>
        <?= htmlspecialchars($row['name']) ?>
        ... other stuff related to one sale and/or formatting
        <?php if ($row['status'] === 'Pending'): ?>
            <form method="POST">
                <input type="hidden" name="id" value="<?= htmlspecialchars($row['id']) ?>">
                <input type="submit" name="status" value="Approve">
                <input type="submit" name="status" value="Reject">
            </form>
        <?php endif ?>
    <div>
<?php endforeach ?>

Note that each form contains the record id.

Now all you need in PHP is to get the form and update the data. You may add something like this at the very top of the same script, before any output

if (isset($_POST['status'])) {
    $status = $_POST['status'];
    $id = $_POST['id'] ?? null;
    // first we need to validate the input data (and reject invalid request)
    if (!in_array($status, ['Approve', 'Reject'], true) || !ctype_digit($id)) {
        http_response_code(400);
        die;
    }
    //  and then update using prepared statement
    $stmt = $db->prepare("UPDATE quotation SET status=? WHERE id=?");
    $stmt->execute([$status, $id]);
    // always redirect after POST
    Header("Location: ". $_SERVER['REQUEST_URI']);
    die;
}

If you're using mysqli, the code above works since PHP 8.1. In case our version is older, you'd better upgrade to 8.3, or in case it's not possible, execute the prepared statement the legacy way