How can PHP developers ensure data integrity and consistency when implementing user-controlled reordering functionality in a web application?

When implementing user-controlled reordering functionality in a web application, PHP developers can ensure data integrity and consistency by validating user input, using transactions to perform database updates, and handling potential race conditions.

// Validate user input
$newOrder = $_POST['new_order'];
if (!is_numeric($newOrder)) {
    // Handle invalid input
}

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

// Update the order of the items in the database
$stmt = $pdo->prepare("UPDATE items SET order = :new_order WHERE id = :item_id");
$stmt->bindParam(':new_order', $newOrder, PDO::PARAM_INT);
$stmt->bindParam(':item_id', $itemId, PDO::PARAM_INT);
$stmt->execute();

// Commit the transaction
$pdo->commit();