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

使用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条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)