This is my Code. I know, the while
loop will work only once. But foreach
does not loop after fetching one value, i.e if I am selecting 3 checkboxes. Only one will pass the values.
if(!empty($_POST["list"]))
{
$connect = @mysql_connect("localhost","***","***") or die (" this error");
mysql_select_db("edu_info")or die ("database does not exist");
foreach($_POST["list"] as $list=> $val)
{
$query=mysql_query("select * from students where id='$val' limit 1");
while($row=mysql_fetch_assoc($query))
{
$user=$row['username'];
$number=$row['pphone'];
$name=$row['name'];
$pname=$row['pname'];
}
echo $user;
echo $number;
echo $name;
echo $pname;
}
}
This is my checkbox in html. The number of checkboxes is not fixed, it depends upon the query.Code below is just to get multiple checkboxes
{
<input type="checkbox" name="list[]" value="<?php echo $id; ?>
}
Why is foreach not fetching values from all the selected checkboxes. Please help. Thanks in advance. :)
答案 0 :(得分:0)
在我看来,你的while循环每次都会覆盖这些值。我没有看到你在检索的值上做了什么,因此,例如:
$user=$row['username'];
可能正在执行三次,但每次都会覆盖之前的值。它总是给你最后选择的值,对吧?如果是这样,那是因为你设置了$user
三次,并且只有在最后一次之后才用它做任何事情。