In the provided PHP code, what improvements can be made to enhance security and prevent potential vulnerabilities?

The provided PHP code is vulnerable to SQL injection attacks due to directly concatenating user input into the SQL query. To enhance security and prevent potential vulnerabilities, we should use prepared statements with parameterized queries. This approach separates the SQL query logic from the user input, making it impossible for an attacker to inject malicious SQL code.

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

// Vulnerable SQL query
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";

// Fixed code using prepared statements
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();

// Process the result as needed