What are the potential security vulnerabilities associated with using user input directly in SQL queries, as seen in the code snippet shared in the forum?

Using user input directly in SQL queries can lead to SQL injection attacks, where malicious users can manipulate the input to execute unauthorized SQL commands. To prevent this, it is important to sanitize and validate user input before using it in SQL queries. One way to do this is by using prepared statements with parameterized queries, which separate the SQL code from the user input.

// Fix using prepared statements with parameterized queries
$pdo = new PDO("mysql:host=localhost;dbname=myDB", "username", "password");

$username = $_POST['username'];
$password = $_POST['password'];

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);

$user = $stmt->fetch();