Query Database Using a Form with Multiple Fields
<p>I have a form with radio buttons that I want to use to get data from a table in a MySQL database and this is the form:</p>

<pre><code>&lt;div class="filter-container"&gt;
&lt;p class="filter-title"&gt;BTU's&lt;/p&gt;&lt;div class="filter"&gt;
&lt;input type="radio" name="choice-0" id="choice-0" tabindex="0" value="" checked="checked" /&gt;
&lt;label for="none"&gt;None&lt;/label&gt;&lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-0" id="choice-0" tabindex="0" value="100000" /&gt;
    &lt;label for="choice-0"&gt;100000&lt;span&gt;(10)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-0" id="choice-0" tabindex="0" value="120000" /&gt;
    &lt;label for="choice-0"&gt;120000&lt;span&gt;(7)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-0" id="choice-0" tabindex="0" value="40000" /&gt;
    &lt;label for="choice-0"&gt;40000&lt;span&gt;(5)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-0" id="choice-0" tabindex="0" value="60000" /&gt;
    &lt;label for="choice-0"&gt;60000&lt;span&gt;(11)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-0" id="choice-0" tabindex="0" value="80000" /&gt;
    &lt;label for="choice-0"&gt;80000&lt;span&gt;(18)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;/div&gt;&lt;div class="filter-container"&gt;
&lt;p class="filter-title"&gt;Efficiency (AFUE)&lt;/p&gt;&lt;div class="filter"&gt;
&lt;input type="radio" name="choice-1" id="choice-1" tabindex="0" value="" checked="checked" /&gt;
&lt;label for="none"&gt;None&lt;/label&gt;&lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-1" id="choice-1" tabindex="1" value="80%" /&gt;
    &lt;label for="choice-1"&gt;80%&lt;span&gt;(21)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-1" id="choice-1" tabindex="1" value="92%" /&gt;
    &lt;label for="choice-1"&gt;92%&lt;span&gt;(8)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-1" id="choice-1" tabindex="1" value="96%" /&gt;
    &lt;label for="choice-1"&gt;96%&lt;span&gt;(17)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-1" id="choice-1" tabindex="1" value="97%" /&gt;
    &lt;label for="choice-1"&gt;97%&lt;span&gt;(5)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;/div&gt;&lt;div class="filter-container"&gt;
&lt;p class="filter-title"&gt;Width&lt;/p&gt;&lt;div class="filter"&gt;
&lt;input type="radio" name="choice-2" id="choice-2" tabindex="0" value="" checked="checked" /&gt;
&lt;label for="none"&gt;None&lt;/label&gt;&lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-2" id="choice-2" tabindex="2" value="14" /&gt;
    &lt;label for="choice-2"&gt;14&lt;span&gt;(4)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-2" id="choice-2" tabindex="2" value="17.5" /&gt;
    &lt;label for="choice-2"&gt;17.5&lt;span&gt;(20)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-2" id="choice-2" tabindex="2" value="21" /&gt;
    &lt;label for="choice-2"&gt;21&lt;span&gt;(19)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-2" id="choice-2" tabindex="2" value="221" /&gt;
    &lt;label for="choice-2"&gt;221&lt;span&gt;(1)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-2" id="choice-2" tabindex="2" value="24.5" /&gt;
    &lt;label for="choice-2"&gt;24.5&lt;span&gt;(7)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;/div&gt;&lt;div class="filter-container"&gt;
&lt;p class="filter-title"&gt;Blower Type&lt;/p&gt;&lt;div class="filter"&gt;
&lt;input type="radio" name="choice-3" id="choice-3" tabindex="0" value="" checked="checked" /&gt;
&lt;label for="none"&gt;None&lt;/label&gt;&lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Multi-Speed" /&gt;
    &lt;label for="choice-3"&gt;Multi-Speed&lt;span&gt;(23)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Multi-Speed ECM" /&gt;
    &lt;label for="choice-3"&gt;Multi-Speed ECM&lt;span&gt;(7)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Single Speed" /&gt;
    &lt;label for="choice-3"&gt;Single Speed&lt;span&gt;(7)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Variable Speed" /&gt;
    &lt;label for="choice-3"&gt;Variable Speed&lt;span&gt;(3)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;div class="filter"&gt;
    &lt;input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Variable Speed ECM" /&gt;
    &lt;label for="choice-3"&gt;Variable Speed ECM&lt;span&gt;(11)&lt;/span&gt;&lt;/label&gt;
    &lt;/div&gt;&lt;/div&gt;
</code></pre>

<p>
</p>

<p>I need to match any combination of values with a category constant.  This is what I am using to process the form:</p>

<pre><code>&lt;?php
if('POST' == htmlspecialchars($_SERVER['REQUEST_METHOD']) ){

    $cat_id = 4001;

    if ( isset($_POST['choice-0']) ) {
        $choice0 = $_POST['choice-0'];
    }

    if ( isset($_POST['choice-1']) ) {
        $choice1 = $_POST['choice-1'];
    }

    if ( isset($_POST['choice-2']) ) {
        $choice2 = $_POST['choice-2'];
    }

    if ( isset($_POST['choice-3']) ) {
        $choice3 = $_POST['choice-3'];
    }

    if ( isset($_POST['choice-4']) ) {
        $choice4 = $_POST['choice-4'];
    }

    if ( isset($_POST['choice-5']) ) {
        $choice5 = $_POST['choice-5'];
    }

    if ( isset($_POST['choice-6']) ) {
        $choice6 = $_POST['choice-6'];
    }

    $query = $connect-&gt;query( "SELECT * FROM PRODUCTS WHERE PR_ProductCategory='$cat_id' AND (PR_UDSearch0='$choice0' OR PR_UDSearch1='$choice1' OR PR_UDSearch2='$choice2' OR PR_UDSearch3='$choice3' OR PR_UDSearch4='$choice4' OR PR_UDSearch5='$choice5' OR PR_UDSearch6='$choice6')" );

    var_dump($query);

}
</code></pre>

<p>?></p>

<p>I have done various SELECT statements to get the data with no luck (including the one above).  I don't want a combination of matches.  For my needs I need matches from one or the other not both.  In other words, I need to use the cat_id as a constant and then match any other combination that I get from the radio buttons (choice 1, or choice 1 and 2, or choice 2 and 4, or choice 3 etc.).  I must be setting it up wrong with the query or something else.  I have googled for days and haven't figured it out yet.  Thanks!!</p>

<p>UPDATE:  I now am trying to create the SELECT statement based on the choices:</p>

<pre><code>$cat_id = 4001;
                    $query_str = "SELECT * FROM PRODUCTS WHERE PR_ProductCategory='$cat_id' AND (";

                    if ( isset($_POST['choice-0'] ) ) {
                        $choice0 = $_POST['choice-0'];
                        $query_str .= "PR_UDSearch0='$choice0'";
                    }

                    if ( isset($_POST['choice-1'] ) ) {
                        $choice1 = $_POST['choice-1'];
                        $query_str .= "OR PR_UDSearch0='$choice1'";
                    }

                    if ( isset($_POST['choice-2'] ) ) {
                        $choice2 = $_POST['choice-2'];
                        $query_str .= "OR PR_UDSearch0='$choice2'";
                    }

                    if ( isset($_POST['choice-3'] ) ) {
                        $choice3 = $_POST['choice-3'];
                        $query_str .= "OR PR_UDSearch0='$choice3'";
                    }

                    if ( isset($_POST['choice-4'] ) ) {
                        $choice4 = $_POST['choice-4'];
                        $query_str .= "OR PR_UDSearch0='$choice4'";
                    }

                    if ( isset($_POST['choice-5'] ) ) {
                        $choice5 = $_POST['choice-5'];
                        $query_str .= "OR PR_UDSearch0='$choice5'";
                    }

                    if ( isset($_POST['choice-6'] ) ) {
                        $choice6 = $_POST['choice-6'];
                        $query_str .= "OR PR_UDSearch0='$choice6'";
                    }

                    $query_str .= ")";
                    $query = $connect-&gt;query( $query_str );
</code></pre>

<p>I think it is closer but I get NULL if something is not selected.</p>

<p>LAST UPDATE:  I figured it out with some help.  I built the SELECT statement based on the selection and used that to get the results. This was my full solution:</p>

<pre><code>    if('POST' == htmlspecialchars($_SERVER['REQUEST_METHOD']) ){

$cat_id = 4001;
$query_str = "SELECT * FROM PRODUCTS WHERE PR_ProductCategory='$cat_id' ";

if ( isset($_POST['choice-0']) ) {
    $choice0 = $_POST['choice-0'];

    if ( $choice0 ) {
        $query_str .= "AND PR_UDSearch0='$choice0'";
    }
}

if ( isset($_POST['choice-1']) ) {
    $choice1 = $_POST['choice-1'];

    if ( $choice1 ) {
        $query_str .= " AND PR_UDSearch1='$choice1'";
    }
}

if ( isset($_POST['choice-2']) ) {
    $choice2 = $_POST['choice-2'];

    if ( $choice2 ) {
        $query_str .= " AND PR_UDSearch2='$choice2'";
    }
}

if ( isset($_POST['choice-3']) ) {
    $choice3 = $_POST['choice-3'];

    if ( $choice3 ) {
        $query_str .= " AND PR_UDSearch3='$choice3'";
    }
}

if ( isset($_POST['choice-4']) ) {
    $choice4 = $_POST['choice-4'];

    if ( $choice4 ) {
        $query_str .= " AND PR_UDSearch4='$choice4'";
    }
}

if ( isset($_POST['choice-5']) ) {
    $choice5 = $_POST['choice-5'];

    if ( $choice5 ) {
        $query_str .= " AND PR_UDSearch5='$choice5'";
    }
}

if ( isset($_POST['choice-6']) ) {
    $choice6 = $_POST['choice-6'];

    if ( $choice6 ) {
        $query_str .= " AND PR_UDSearch6='$choice6'";
    }
}

$query_str .= "";
$query = $connect-&gt;query( $query_str );

while ($row = mysqli_fetch_array($query)) {

$sku    = $row[PR_SKU];
$img    = $row[PR_URLofThumb];
$desc   = $row[PR_Description];
$price  = $row[PR_UnitPrice];

echo '&lt;div class="box"&gt;
&lt;div class="box-image"&gt;
&lt;a href="/cgi-bin/commerce.cgi?preadd=action&amp;key=' . $sku . '"&gt;&lt;img src="' . $img . '" /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;Price:' .  $price . '&lt;/p&gt;
&lt;a href="/cgi-bin/commerce.cgi?preadd=action&amp;key=' . $sku . '"&gt;' . $desc . '&lt;/a&gt;
&lt;/div&gt;';

 }
}
</code></pre>