1

我很难将基于多个复选框选择的数据显示到表格中。我相信 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>' . '&nbsp' . '</td>';
        echo '<td rowspan="5">' . $row['descr'] . '</td>';
        echo '<td><b>' . 'Total weight' . '<b></td>';
        echo '<td>' . '&nbsp' . '</td><td>' . '&nbsp' . '</td></tr>';

        echo '<td>' . '&nbsp' . '</td>';
        echo '<td colspan="3">' . '&nbsp' . '</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>";
}
4

2 回答 2

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
于 2013-08-31T23:49:59.463 回答
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);
于 2013-08-31T23:50:48.500 回答