What are the common pitfalls to avoid when creating a search script in PHP?

One common pitfall when creating a search script in PHP is not properly sanitizing user input, which can lead to SQL injection attacks. To avoid this, always use prepared statements or parameterized queries when interacting with a database.

// Example of using prepared statements to avoid SQL injection

$searchTerm = $_GET['search'];

$stmt = $pdo->prepare("SELECT * FROM products WHERE name LIKE :searchTerm");
$stmt->execute(['searchTerm' => "%$searchTerm%"]);

$results = $stmt->fetchAll();