我很难将基于多个复选框选择的数据显示到表格中。
我相信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>";
}
答案 0 :(得分:1)
$w
是一个数组,而不是一个值,因此您尝试在查询中传递数组,它应该是这样的:
$palnums=implode(',',$w);//this will transform your array to list of values separated by comma
$rrr = mysql_query("SELECT * FROM pl_tab WHERE pal_num in ($palnums)");//this will check if pal_num is in your array
答案 1 :(得分:1)
在post2.php中,您尝试在数据库查询中使用数组。尝试:
$rrrsql = "SELECT * FROM pl_tab WHERE ";
$wCount = count($w);
$n = 1;
foreach ($w as $item) {
$rrrsql .= "pal_num='" . $item . "'";
if ($n != $wCount) {
$rrrsql .= " OR "
}
$n++;
}
$rrr = mysql_query($rrrsql);