简体   繁体   中英

PHP: Multiple selects from MYSQL database?

I'm trying to search MYSQL database and display the results in my PHP page.

I am using Multiple Select dropdowns to search the mysql database.

Like so:

<select name="keyword">
<option></option>
<option valeu="Some value">Some value</option>
<option valeu="Some value">Some value</option>
<option valeu="Some value">Some value</option>
<option valeu="Some value">Some value</option>
</select>

<select name="keyword">
<option></option>
<option valeu="Some value">Some value</option>
<option valeu="Some value">Some value</option>
<option valeu="Some value">Some value</option>
<option valeu="Some value">Some value</option>
</select>

and every select dropdown looks the same with different values etc..

My mysql table looks like this:

id  Material  Lamination  cat_name  sub_cat_name  product_name
1   metal     gloss          metals       metal sheets    flat metal
2                            metals       metal sheets    flat metal
3   wood      gloss          woods       wood  sheets    flad wood
4                            woods       wood sticks      curved wood

etc etc...

As you can see in the example above, some of the products don't have the Material and Lamination values.

So, i search the MYSQL like this:

    if(isset($_POST['keyword'])){

            foreach($_POST['keyword'] as $c){
                if(!empty($c)){
    $sql3 ="SELECT * FROM product_details WHERE sub_cat_name='$currentproduct' AND (Lamination LIKE '$c' OR Material LIKE '$c')";


    $query3 = mysqli_query($db_conx, $sql3);

    if (!$query3) {
        die(mysqli_error($db_conx));
    }


    $productCount3 = mysqli_num_rows($query3); // count the output amount
    if ($productCount3 > 0) {
        while($row3 = mysqli_fetch_array($query3, MYSQLI_ASSOC)){ 

......................................

However, when I get the results back from mysql database, it displays them in a very strange way. but the main issue is it displays other items with the sub_cat_name value even though those items do not have the Material and Lamination values in them!

I understand that what i am doing in the MYSQL query is wrong but i cannot figure out the right way of doing this!

Baically, what I am trying to achieve is to only display the items that some criteria's have been selected from the dropdown lists....

Could someone please advise on this?

Thanks in advance.

So you have a bunch of keywords, and you want to select all the products where either the material or the lamination matches at least one of the keyword.

One easy way to do it without changing too much of your code would be to simply concatenate a bunch of OR into your mysql query.

$sql3 = "SELECT * FROM product_details WHERE sub_cat_name='$currentproduct' AND (";

$delimiter = "";
foreach($_POST['keyword'] as $c){
    if(!empty($c)){
        $c = mysqli_real_escape_string($db_conx, $c);
        $sql3 .=  $delimiter . " (Lamination='$c' OR Material='$c')";
        $delimiter  = " OR";
    }
}

$sql3 .= ")";

This will give you a rather long query, but it will have all the conditions in it.

SELECT * FROM product_details WHERE sub_cat_name='product' AND ( 
(Lamination='metal' OR Material='metal') OR (Lamination='wood' OR 
Material='wood') OR (Lamination='gloss' OR Material='gloss') OR 
(Lamination='glass' OR Material='glass'))

However, if you want to also allow the user to select no keywords, then you need to do a separate query without the AND .

One thing to note is that I added in a mysqli_real_escape_string to protect your query from MySQL injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM