What are the best practices for handling concurrent user actions in a PHP-based seat reservation system?

Issue: When multiple users try to reserve the same seat simultaneously in a PHP-based seat reservation system, it can lead to conflicts and inconsistencies in the data. To handle concurrent user actions effectively, we can use database transactions to ensure that only one user can reserve a seat at a time, preventing conflicts and maintaining data integrity. PHP Code Snippet:

// Start a transaction
$pdo->beginTransaction();

// Check if the seat is available
$seat = $pdo->query("SELECT * FROM seats WHERE id = $seat_id FOR UPDATE")->fetch();

if ($seat['status'] == 'available') {
    // Update the seat status to 'reserved'
    $pdo->exec("UPDATE seats SET status = 'reserved' WHERE id = $seat_id");

    // Commit the transaction
    $pdo->commit();
    echo "Seat reserved successfully!";
} else {
    // Rollback the transaction if the seat is not available
    $pdo->rollBack();
    echo "Seat is already reserved. Please choose another seat.";
}