In PHP, what are some recommended approaches for validating and sanitizing user input of prices to ensure consistency and prevent data corruption in MySQL databases?

When dealing with user input of prices in PHP, it is important to validate and sanitize the input to ensure consistency and prevent data corruption in MySQL databases. One recommended approach is to use PHP's filter_var() function with the FILTER_VALIDATE_FLOAT filter to validate the input as a float number. Additionally, you can use prepared statements when inserting the price into the database to prevent SQL injection attacks.

// Validate and sanitize user input for price
$user_input_price = $_POST['price'];
$filtered_price = filter_var($user_input_price, FILTER_VALIDATE_FLOAT);

// Insert the sanitized price into the database using prepared statements
$stmt = $pdo->prepare("INSERT INTO products (price) VALUES (:price)");
$stmt->bindParam(':price', $filtered_price, PDO::PARAM_STR);
$stmt->execute();