How can SQL Injection vulnerabilities be addressed in PHP code like the example provided in the forum thread?

SQL Injection vulnerabilities can be addressed in PHP code by using prepared statements with parameterized queries instead of directly inserting user input into SQL queries. This helps to prevent malicious SQL injection attacks by separating the SQL query logic from the user input data.

// Original vulnerable code
$username = $_POST['username'];
$password = $_POST['password'];

$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);

// Fixed code using prepared statements
$username = $_POST['username'];
$password = $_POST['password'];

$query = "SELECT * FROM users WHERE username=? AND password=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();