2013-08-31 119 views
1

我真的很难显示数据基于多个复选框选择到表.. 我相信post2.php上的while循环可能不是正确的解决方案。 我还注意到的是,代码工作时,字符串发布,但不是数组(多选)input type="checkbox" name="pal_num[]"例如它的工作原理没有[] 任何人都可以帮忙吗?显示基于复选框的数据选择到html表

<?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>"; 
} 
+0

你有什么结果 –

+0

作为结果是什么?的rrr? – SweetJC

+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 
+0

非常感谢。它帮助我进一步移动。 – SweetJC

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); 
+0

谢谢。这也有帮助。 – SweetJC

相关问题