我很难将基于多个复选框选择的数据显示到表格中。我相信 post2.php 上的 while 循环可能不是解决这个问题的正确方法。我还注意到,当字符串时代码有效已发布,但不是数组(多选)input type="checkbox" name="pal_num[]"
,例如它可以在没有[]
任何人帮助的情况下工作吗?
<?php
$l = $_POST['LT'];
$pals = '';
$r = mysql_query("SELECT DISTINCT pal_num FROM pl_tab WHERE lt_num='$l'");
while ($row = mysql_fetch_assoc($r)) {
$pals .= '<input type="checkbox" name="pal_num[]" value="'
. $row['pal_num'] . '">' . $row['pal_num'] . '<br>';
}
if ($pal == '') {
echo '';
} else {
echo '<form name="get_pal" action="post2.php" method="POST">';
echo $pals;
echo '<input type="submit" name="post" value="Go!">';
echo '</form>';
}
?>
post2.php:
if (isset($_POST['pal_num'])) {
var_dump($_POST);
//all of 3 options checked
//array(2) { ["pal_num"]=> array(3) { [0]=> string(3) "111" [1]=> string(3) "222" [2]=> string(3) "333" } ["post"]=> string(3) "Go!" }
mysql_connect(DB_HOST, DB_UNAME, DB_PWD) or die('Database error!!');
mysql_select_db(DB_NAME);
echo '</br>';
echo "<table border='1'>
<tr>
<th width=80 height=25>L num</th>
<th width=110 height=25>Descr</th>
<th width=90 height=25>Pal num</th>
<th width=60 height=25>weight 1</th>
<th width=60 height=25>weight 2</th>
</tr>";
$w = $_POST['pal_num'];
$rrr = mysql_query("SELECT * FROM pl_tab WHERE pal_num='$w'");
var_dump($w);
//all of 3 options checked
//array(3) { [0]=> string(3) "111" [1]=> string(3) "222" [2]=> string(3) "333" }
while ($row = mysql_fetch_array($rrr)) {
echo '<tr><td>' . ' ' . '</td>';
echo '<td rowspan="5">' . $row['descr'] . '</td>';
echo '<td><b>' . 'Total weight' . '<b></td>';
echo '<td>' . ' ' . '</td><td>' . ' ' . '</td></tr>';
echo '<td>' . ' ' . '</td>';
echo '<td colspan="3">' . ' ' . '</td>';
echo '<tr><td>' . $row['l_num'] . '</td>';
echo '<td>' . $row['pal_num'] . '</td>';
echo '<td>' . $row['weight 1'] . '</td><td>'
. $row['weight 2'] . '</td></tr>';
}
echo "</table>";
}