What potential security risks are present in the PHP code provided?
The potential security risk in the provided PHP code is the use of user input directly in the SQL query without proper sanitization, which can lead to SQL injection attacks. To solve this issue, you should use prepared statements with parameterized queries to prevent SQL injection vulnerabilities.
// Original code with SQL injection vulnerability
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
// Fixed code using prepared statements
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();