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.