PHP带下拉框的高级搜索表单开发故障排查
Hey there! Let's get that combined search working for you. It sounds like you've nailed the category dropdown filtering, but tying it to the search input for a joint query is where things are stuck. Let's break this down step by step.
First: Double-Check Your Form (index.php)
Make sure your form is properly sending both the selected category and search keyword to your backend. Here's a corrected example of what that might look like:
<form method="POST" action="advanced-search.php"> <!-- Category Dropdown --> <select name="category" id="category"> <option value="">All Categories</option> <option value="tech">Technology</option> <option value="retail">Retail</option> <!-- Add your other categories here --> </select> <!-- Search Input --> <input type="text" name="search_keyword" placeholder="Search company names..." value="<?php echo isset($_POST['search_keyword']) ? htmlspecialchars($_POST['search_keyword']) : ''; ?>"> <button type="submit">Search</button> </form>
Key things to confirm here:
- Both fields have a
nameattribute (critical for passing data to the backend) - The form uses
POSTmethod (safer for search queries than GET) - We're preserving the search input value on submit for better UX
Next: Fix the Query Logic (advanced-search.php)
The main issue is likely that your current query isn't combining both conditions. We need to build a dynamic SQL query that includes both the category filter and the company name search—while also handling cases where one or both fields might be empty.
Here's a secure, working example using MySQLi (adjust for PDO if that's what you're using):
// Assuming you already have your database connection set up ($conn) $category = $_POST['category'] ?? ''; $searchKeyword = trim($_POST['search_keyword'] ?? ''); // Start with a base query that always evaluates to true (makes dynamic WHERE clauses easier) $sql = "SELECT * FROM your_table_name WHERE 1=1"; $params = []; $paramTypes = ''; // Add category filter if a category is selected if (!empty($category)) { $sql .= " AND category = ?"; $params[] = $category; $paramTypes .= 's'; // 's' for string type } // Add company name search if a keyword is entered if (!empty($searchKeyword)) { $sql .= " AND company_name LIKE ?"; $params[] = "%{$searchKeyword}%"; // Wildcards for partial matches $paramTypes .= 's'; } // Prepare and execute the query (prevents SQL injection!) $stmt = $conn->prepare($sql); if (!empty($params)) { // Bind parameters dynamically $stmt->bind_param($paramTypes, ...$params); } $stmt->execute(); $result = $stmt->get_result(); // Now you can loop through $result to display your matches while ($row = $result->fetch_assoc()) { echo "<div>{$row['company_name']} ({$row['category']})</div>"; }
Why This Works:
- We use
1=1as a base so we can safely addANDconditions without worrying about syntax errors if no filters are applied - Parameter binding ensures we avoid SQL injection (never concatenate user input directly into your SQL!)
- It handles all edge cases:
- Only category selected: filters by category
- Only search keyword entered: searches company names
- Both filled: returns results that match both the category and the company name keyword
- Neither filled: returns all results (you can adjust this to show a message instead if needed)
Quick Troubleshooting Tips
- If you're still getting no results, echo out the final
$sqland$paramsto debug what query is being run - Ensure your database column names match exactly (e.g.,
categoryvsCategory—SQL is case-sensitive on some systems) - Check that your dropdown options have valid
valueattributes (not empty unless you want an "All" option)
内容的提问来源于stack exchange,提问作者Sam Smith

