What are some best practices for handling user input and form submissions when implementing a feature to rearrange records in a table using PHP?

When implementing a feature to rearrange records in a table using PHP, it is important to properly handle user input and form submissions to prevent security vulnerabilities such as SQL injection attacks. One best practice is to validate and sanitize user input before processing it. Additionally, use prepared statements when interacting with the database to prevent SQL injection.

// Validate and sanitize user input
$newOrder = filter_input(INPUT_POST, 'new_order', FILTER_SANITIZE_NUMBER_INT);

// Use prepared statements to update records in the database
$stmt = $pdo->prepare("UPDATE records SET order = :new_order WHERE id = :record_id");
$stmt->bindParam(':new_order', $newOrder, PDO::PARAM_INT);
$stmt->bindParam(':record_id', $recordId, PDO::PARAM_INT);
$stmt->execute();