What potential issues can arise when using PHP to interact with a database and manage user data?
Issue: SQL injection attacks can occur if user input is not properly sanitized before being used in database queries. To prevent this, always use prepared statements or parameterized queries when interacting with the database.
// Using prepared statements to prevent SQL injection
// Assuming $conn is the database connection object
// Prepare a statement
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
// Set parameters and execute
$username = $_POST['username'];
$stmt->execute();
// Fetch results
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Process the results
}
// Close statement and connection
$stmt->close();
$conn->close();