What are some common pitfalls for PHP beginners when creating a search function on a website?

One common pitfall for PHP beginners when creating a search function on a website is not properly sanitizing user input, which can leave the website vulnerable to SQL injection attacks. To solve this issue, always use prepared statements or parameterized queries to prevent malicious SQL injection.

// Example of using prepared statements for search function
$searchTerm = $_GET['search'];
$stmt = $pdo->prepare("SELECT * FROM products WHERE name LIKE :searchTerm");
$stmt->execute(['searchTerm' => "%$searchTerm%"]);
$results = $stmt->fetchAll();
foreach($results as $result) {
    // Display search results
}