简体   繁体   中英

Select multiple items from mysql database based on checkboxes using PHP?

I am trying to get multiple items from mysql database based on the checkboxes select.

Example:

1- user selects 3 checkboxes on the page and clicks on the submit button. 2- on the next page, those 3 products would show.

my current code is like this:

First Page:

<div align="center">
<input style="float:left;" type="checkbox" name="check_list[]" value="'.$product_name.'" />
<img width="67" src="../images/'.$id.'.jpg"  /><br />
'.$product_name.'
</div>

the code above is in a while loop and it works fine. I get all the products from the mysql database as it should.

second page:

<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
//echo $selected."</br>";



include "../config/connect.php";
// This block grabs the whole list for viewing
$products_list = "";
$sql = "SELECT * FROM products WHERE product_name='$selected'";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $gender = $row["gender"];
             $products_list .= '<div align="center">
<input style="float:left;" type="checkbox" name="check_list[]" value="'.$product_name.'" />
<img width="67" src="../images/'.$id.'.jpg"  /><br />
'.$product_name.'
</div>';
    }
} else {
    $products_list .= "You have nothing";
}
}
}
}
?>
<?php echo $products_list; ?>

the code on the second page only echo's the last checked item!

but i need to display all the items checked on the first page.

could someone please help me out with this?

Thanks.

Don't do

$products_list = "";

inside your loop. Move it prior to

foreach($_POST['check_list'] as $selected){

Its printing the last checked item because you are resetting the string you are echoing every time the loop interates. Use this code.

<?php
if(isset($_POST['submit'])){//to run PHP script on submit
    if(!empty($_POST['check_list'])){
    // Loop to store and display values of individual checked checkbox.
    $products_list = "";
        foreach($_POST['check_list'] as $selected){
        //echo $selected."</br>";



        include "../config/connect.php";
        // This block grabs the whole list for viewing
        $sql = "SELECT * FROM products WHERE product_name='$selected'";
        $query = mysqli_query($db_conx, $sql);
        $productCount = mysqli_num_rows($query); // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
                    $id = $row["id"];
                    $product_name = $row["product_name"];
                    $gender = $row["gender"];
                    $products_list .= '<div align="center">
                    <input style="float:left;" type="checkbox" name="check_list[]" value="'.$product_name.'" />
                    <img width="67" src="../images/'.$id.'.jpg"  /><br />
                    '.$product_name.'
                    </div>';
                }
            } else {
                $products_list .= "You have nothing";
            }
        }
    }
}
?>
<?php echo $products_list; ?>

I moved $products_list outside the foreach loop.

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