What is the potential issue with using $_POST variables in PHP code?

Using $_POST variables directly in PHP code can pose a security risk as it makes the code vulnerable to SQL injection attacks. To mitigate this risk, it is recommended to sanitize and validate user input before using it in the code. This can be done by using functions like filter_input() or prepared statements in SQL queries.

// Sanitize and validate user input from $_POST
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);

// Use prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();