How can developers ensure that spaces in data values are properly handled in SQL queries to avoid unexpected behavior?

Spaces in data values can cause unexpected behavior in SQL queries if not handled properly. To avoid this, developers should always sanitize user input and use prepared statements to prevent SQL injection attacks. Additionally, developers should trim any leading or trailing spaces from data values before using them in SQL queries to ensure accurate results.

// Example PHP code snippet to handle spaces in data values in SQL queries
$user_input = " John Doe "; // User input with leading and trailing spaces
$clean_input = trim($user_input); // Remove leading and trailing spaces
$stmt = $pdo->prepare("SELECT * FROM users WHERE name = :name");
$stmt->bindParam(':name', $clean_input, PDO::PARAM_STR);
$stmt->execute();