2

So I'm trying to apply a filter for a sql query which displays the mag wheels based on the manufacturer selected.

So far it generates a dropdown list for the html form, and is supposed to pass it back using $_POST and then is applied to the sql query to redisplay the page. I have something similar working but that uses an array and checkboxs instead of the option> box used in this form.

currently It doesn't apply any filter and actually breaks the loop which generates the wheels to be displayed...

// Getting manufacturer to create the filter list.
    $filterManu = "SELECT DISTINCT manufacturer FROM wheels";
    $filterManuResult = mysqli_query($db, $filterManu) or die("Error in Selecting " . mysqli_error($db));

// Applying the filtering
if (isset($_POST['manu'])) {

    $option = $_POST['manu'];
    $featuredsql = "SELECT * FROM wheels WHERE manufacturer = " . $option . " ORDER BY diam";
}

$featured = $db->query($featuredsql);

Form that generates the list of manufacturers to select from and submits the post from the form.

    <form name="manuFilter" method="POST">
        <div class="form-group"></div>
            <select name="manu">
             <option selected disabled>Select a manufacturer</option>
             <?php while($rowManu = mysqli_fetch_assoc($filterManuResult)) : ?>
                <option name="manu" value="<?php echo $rowManu['manufacturer']; ?>"><?php echo $rowManu['manufacturer']; ?></option>
             <?php endwhile; ?>
            </select>    

           <div class="form-group"></div>
           <button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Apply</button>
    </form>

3 Answers 3

2

Two things to b updated:

    $featuredsql = "SELECT * FROM wheels WHERE manufacturer = '" . $option . "' ORDER BY diam";<br>

(I added a " ' ")
and options take the following syntax:

    <option value="option_value">bla bla bla

where bla bla bla is the name

Sign up to request clarification or add additional context in comments.

Comments

0

options shouldn't have name attributes; this might be the sole reason your post values break - especially since the option name is the same as the selects.

Comments

0

I think you have problem with $featuredsql. Display your query $featuredsql and check that it is working fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.