douzhanyan5015 2014-08-27 21:56
浏览 41
已采纳

使用PHP基于复选框从mysql数据库中选择多个项目?

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.

  • 写回答

2条回答 默认 最新

  • doukanwen4114 2014-08-27 22:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1