How can one ensure proper security measures when updating a database with user input in PHP?

When updating a database with user input in PHP, it is essential to use prepared statements to prevent SQL injection attacks. This involves binding parameters to the query rather than directly inserting user input. Additionally, input validation should be performed to ensure that only expected data is being submitted.

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

// Prepare a SQL statement with placeholders
$stmt = $pdo->prepare("UPDATE users SET name = :name WHERE id = :id");

// Bind parameters to the placeholders
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':id', $_POST['id']);

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