What are common pitfalls when using PHP for updating database records based on user input?

One common pitfall when using PHP for updating database records based on user input is not properly sanitizing the user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements to bind parameters and execute queries safely.

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare the update query with placeholders
$stmt = $mysqli->prepare("UPDATE table SET column = ? WHERE id = ?");

// Bind parameters and execute the query
$stmt->bind_param("si", $user_input, $id);
$user_input = $_POST['user_input'];
$id = $_POST['id'];
$stmt->execute();

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