What are common pitfalls to avoid when using PHP to manipulate database records based on user selections?

One common pitfall to avoid when manipulating database records based on user selections in PHP is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements with parameterized queries to securely interact with the database.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Sanitize user input
$user_id = filter_input(INPUT_POST, 'user_id', FILTER_SANITIZE_NUMBER_INT);

// Prepare a statement with a parameterized query
$stmt = $pdo->prepare("UPDATE users SET status = 'active' WHERE id = :user_id");
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();