What are some potential pitfalls of using the method shown in the PHP code snippet?
The potential pitfall of using the method shown in the PHP code snippet is that it is vulnerable to SQL injection attacks. The code directly interpolates user input into the SQL query without sanitizing or escaping it, which can allow malicious users to manipulate the query and potentially access or modify sensitive data in the database. To prevent SQL injection attacks, you should use prepared statements with parameterized queries to securely handle user input.
// Original vulnerable code snippet
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);
// Fixed code snippet using prepared statements
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username=? AND password=?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "ss", $username, $password);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);