What are the potential pitfalls of not properly handling context switches when passing user input values into SQL queries in PHP?

Improper handling of context switches when passing user input values into SQL queries in PHP can lead to SQL injection attacks, where malicious users can manipulate the queries to access or modify sensitive data. To prevent this, always use prepared statements and parameterized queries to sanitize and validate user input before executing the SQL query.

// Example of properly handling context switches in PHP using prepared statements

// Assuming $conn is the database connection object

// User input value
$userInput = $_POST['user_input'];

// Prepare the SQL statement with a placeholder for the user input
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $userInput);

// Execute the prepared statement
$stmt->execute();

// Fetch the results
$result = $stmt->get_result();

// Process the results
while ($row = $result->fetch_assoc()) {
    // Handle the fetched data
}

// Close the statement and connection
$stmt->close();
$conn->close();