How can SQL injection vulnerabilities be avoided when updating database values in PHP?

SQL injection vulnerabilities can be avoided when updating database values in PHP by using prepared statements with parameterized queries. This approach ensures that user input is treated as data rather than executable SQL code, preventing malicious users from manipulating the query to perform unauthorized actions on the database.

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL update statement with a parameterized query
$stmt = $pdo->prepare("UPDATE users SET email = :email WHERE id = :id");

// Bind the parameters and execute the query
$stmt->bindParam(':email', $newEmail);
$stmt->bindParam(':id', $userId);
$stmt->execute();