What potential pitfalls should be considered when allowing users to input multiple keywords in a search function in PHP?
Allowing users to input multiple keywords in a search function can lead to potential security vulnerabilities such as SQL injection attacks if the input is not properly sanitized. To mitigate this risk, it is important to sanitize and validate the user input before using it in any database queries.
// Sanitize and validate user input for multiple keywords in a search function
$keywords = isset($_GET['keywords']) ? filter_var($_GET['keywords'], FILTER_SANITIZE_STRING) : '';
// Use prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM table WHERE column LIKE :keywords");
$stmt->execute(['keywords' => "%$keywords%"]);
// Fetch the results
$results = $stmt->fetchAll();