Home > Backend Development > PHP Tutorial > How to Build a Search Form with Multiple Search Parameters in PHP?

How to Build a Search Form with Multiple Search Parameters in PHP?

Linda Hamilton
Release: 2024-12-17 02:08:23
Original
845 people have browsed it

How to Build a Search Form with Multiple Search Parameters in PHP?

Search Form with One or More (Multiple) Parameters

Problem:

You have a search form that allows users to input multiple search parameters, such as student ID, name, major, or college. You want to be able to search for students using any combination of these parameters.

Answer:

To enable searching with multiple parameters, dynamically build the SQL WHERE clause based on the parameters entered by the user.

Solution:

  1. Use PHP's PDO (Preferred Method):

    • Create an array to hold the WHERE conditions.
    • Iterate through each search parameter and add a corresponding WHERE condition to the array.
    • Implode the array to create a single WHERE clause.
    • Prepare and execute a PDO statement using the WHERE clause and bind the parameters.
    $wheres = [];
    $params = [];
    if (!empty($_GET['id'])) {
        $wheres[] = 'a.uid = :uid';
        $params[':uid'] = $_GET['id'];
    }
    if (!empty($_GET['major'])) {
        $wheres[] = 'a.major = :major';
        $params[':major'] = $_GET['major'];
    }
    if (!empty($_GET['name'])) {
        $wheres[] = 'b.name LIKE :name';
        $params[':name'] = '%'.$_GET['name'].'%';
    }
    // ...continue for additional parameters
    
    $sql = "SELECT * FROM user_details AS a JOIN user AS b ON a.uid = b.id";
    if (!empty($wheres)) {
        $sql .= " WHERE " . implode(' AND ', $wheres);
    }
    $stmt = $db->prepare($sql);
    $stmt->execute($params);
    Copy after login
  2. Modify Existing MySQLi Approach:

    • Check for each non-empty parameter and append a corresponding WHERE condition to the SQL query.
    • Use AND to combine the conditions.
    $sql = "SELECT * FROM user_details a, user b WHERE a.uid = b.id";
    if (!empty($_GET['id'])) {
        $sql .= " AND a.uid = '" . $_GET['id'] . "'";
    }
    if (!empty($_GET['major'])) {
        $sql .= " AND a.major = '" . $_GET['major'] . "'";
    }
    if (!empty($_GET['name'])) {
        $sql .= " AND b.name LIKE '%" . $_GET['name'] . "%'";
    }
    // ...continue for additional parameters
    
    $result = mysqli_query($db, $sql);
    Copy after login

The above is the detailed content of How to Build a Search Form with Multiple Search Parameters in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template