search

Home  >  Q&A  >  body text

The search contains only results for the submitted search input value.

<p>Hi, I'd be happy to help you with your search script if possible. </p><p>I have a search function that retrieves data about vehicles from the database using three types of search inputs and a submit button. </p><p>Input 1: option list search (searcha)</p><p>Input 2: text search (searchb)</p><p>Input 3: two single Select button to select search (you can select one or two buttons)</p><p>There is also a submit button. </p><p>When I submit a search, the following PHP code runs. It will display the results, but only if they match the first correct result in the database. I want it to be able to search results that only contain the submitted search input value. </p><p>For example, if I select an option from input 1 and a radio button from input 3, the search should only find rows containing both values ​​(if that makes sense). </p><p><br /></p> <pre class="brush:php;toolbar:false;"><? //Vehicles if (in_array($_POST['searcha'], $search_a_option_list)) { $stmt = $pdo->prepare('SELECT * FROM coun WHERE inth = ?'); $stmt->execute([ $_POST['searcha'] ]); $search_results = $stmt->fetchAll(PDO::FETCH_ASSOC); } else if ($_POST['searchb']) { $stmt = $pdo->prepare('SELECT * FROM coun WHERE bunty LIKE ?'); $stmt->execute([ '%' . $_POST['searchb'] . '%' ]); $search_results = $stmt->fetchAll(PDO::FETCH_ASSOC); } else if (isset($_POST['radio1'], $_POST['radio2'])) { $stmt = $pdo->prepare('SELECT * FROM coun WHERE ref = ? OR ref1 = ?'); $stmt->execute([ 'In', 'On' ]); $search_results = $stmt->fetchAll(PDO::FETCH_ASSOC); } else if (isset($_POST['radio1'])) { $stmt = $pdo->prepare('SELECT * FROM coun WHERE ref = ?'); $stmt->execute([ 'In' ]); $search_results = $stmt->fetchAll(PDO::FETCH_ASSOC); } else if (isset($_POST['radio2'])) { $stmt = $pdo->prepare('SELECT * FROM coun WHERE ref1 = ?'); $stmt->execute([ 'On' ]); $search_results = $stmt->fetchAll(PDO::FETCH_ASSOC); } else { $error = 'No options selected!'; } ?></pre> <p>Is there an easy way to adjust this script to reflect the entered search? I'm just starting to learn PHP, so a proper guide would be perfect. Thanks. </p><p><br /></p>
P粉541551230P粉541551230578 days ago613

reply all(1)I'll reply

  • P粉930534280

    P粉9305342802023-07-26 09:07:18

    First of all, I don't understand how to select two radio buttons at the same time, do you mean checkboxes? Regarding your question - anyway, you should execute a query, but build it based on your $_POST value

    maybe like this:


    // make an empty array to hold all the conditions
    $whereValues = [];
    
    // populate the array with conditions
    if (in_array($_POST['searcha'], $search_a_option_list)) {
        $whereValues[] = "inth = '$_POST[searcha]'";
    } else if ($_POST['searchb']) {
        $whereValues[] = "bunty LIKE '%$_POST[searchb]%'"
    } else if (isset($_POST['radio1'], $_POST['radio2'])) {
        $whereValues[] = "ref = 'In' OR ref1 = 'On'"
    } else if (isset($_POST['radio1'])) {
        $whereValues[] = "ref = 'In'"
    } else if (isset($_POST['radio2'])) {
        $whereValues[] = "ref1 = 'On'"
    } 
    
    // if we have something then build the result string of `WHERE` conditions
    if ($whereValues) {
        $where = implode(' AND ', $whereValues);
        $stmt = $pdo->prepare("SELECT * FROM coun WHERE $where");
        $stmt->execute();
        $search_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    } else {
        $error = 'No options selected!';
    }

    reply
    0
  • Cancelreply