Home >Backend Development >PHP Tutorial >How Can I Modify a Single-Parameter Search Form to Accept Multiple Search Parameters?
In your search form implementation, you allow users to input only a single parameter for searching. To enable users to specify multiple parameters, consider modifying your code to dynamically build the search query based on the provided parameters.
if ( (isset($_POST['id']) && !empty($_POST['id'])) || (isset($_POST['major']) && !empty($_POST['major'])) || (isset($_POST['college']) && !empty($_POST['college'])) || (isset($_POST['name']) && !empty($_POST['name'])) ) { $params = array(); $wheres = array(); if (isset($_POST['id']) && !empty($_POST['id'])) { $wheres[] = 'a.uid = :uid'; $params[':uid'] = $_POST['id']; } if (isset($_POST['major']) && !empty($_POST['major'])) { $wheres[] = 'a.major = :major'; $params[':major'] = $_POST['major']; } if (isset($_POST['college']) && !empty($_POST['college'])) { $wheres[] = 'a.college = :college'; $params[':college'] = $_POST['college']; } if (isset($_POST['name']) && !empty($_POST['name'])) { $wheres[] = 'b.name LIKE :name'; $params[':name'] = '%'.$_POST['name'].'%'; } $sql = "SELECT * FROM user_details AS a JOIN user AS b ON a.uid = b.id"; if (!empty($wheres)) { $sql .= " WHERE " . implode(' AND ', $wheres); } // ... prepare and execute the query ... } else { // Handle the case when no parameters are set }
// Display the results as before, using a loop and echoing HTML for each result.
By dynamically building the query based on the input parameters, users can now specify multiple parameters in your search form, and the results page will display the students that match all of the specified criteria.
The above is the detailed content of How Can I Modify a Single-Parameter Search Form to Accept Multiple Search Parameters?. For more information, please follow other related articles on the PHP Chinese website!