使用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.
我试图根据复选框选择从mysql数据库中获取多个项目。 p>
示例: p>
1-用户在页面上选中3个复选框,然后单击提交按钮。 2-在下一页上,将显示这3个产品。 p>
我当前的代码是这样的: em> p>
第一页: strong> p>
上面的代码位于 第二页: strong> p>
第二页上的代码只有echo是最后检查的项目! p>
但是我需要显示全部 在第一页上检查的项目。 p>
有人可以帮我解决这个问题吗? p>
谢谢。 p>
div >
nn&lt; div align =“center”&gt;
&lt; input style =“float:left;” type =“checkbox”name =“check_list []”value =“'。$ product_name。'”/&gt;
&lt; img width =“67”src =“../ images /'.$ id。'。jpg “/&gt;&lt; br />
'.$product_name.'
</div>
while loop code>,它工作正常。 我应该从mysql数据库中获取所有产品。 p>
&lt; ?php
if(isset($ _ POST ['submit'])){//在submit
上运行PHP脚本(!empty($ _ POST ['check_list'])){
//循环存储和显示 单个复选框的值。
($ _ POST ['check_list']为$ selected){
// echo $ selected。“&lt; / br&gt;”;
包括“../config /connect.php";
//此块抓取整个列表以查看
$ products_list =“”;
$ sql =“SELECT * FROM products WHERE product_name ='$ selected'”;
$ query = mysqli_query($ db_conx,$ sql);
$ productCount = mysqli_num_rows($ query); //计算输出量
if($ productCount&gt; 0){
while($ row = mysqli_fetch_array($ query,MYSQLI_ASSOC)){
$ id = $ row [“id”];
$ product_name = $ row [“product_name”];
$ gender = $ row [“gender”];
$ products_list。='&lt; div align =“center”&gt;
&lt; input style =“float:left;” type =“checkbox”name =“check_list []”value =“'。$ product_name。'”/&gt;
&lt; img width =“67”src =“../ images /'.$ id。'。jpg “/&gt;&lt; br />
'.$product_name.'
</div>';;n}
} else {
$ products_list。=”你什么都没有“;
} \ n}
}
}
?&gt;
&lt;?php echo $ products_list; ?&gt;
code> pre>
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.
Don't do
$products_list = "";
inside your loop. Move it prior to
foreach($_POST['check_list'] as $selected){