1

I'm having really hard times displaying data based on multiple checkbox selection into a table.. I believe the while loop on post2.php might not be the right solution for this.. What i also have noticed is that the code works when string is posted, but not the array (multiple selection) input type="checkbox" name="pal_num[]" e.g. it works without [] Can anybody help please?

<?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>";
}
2
  • what do you have as result Commented Aug 31, 2013 at 23:45
  • as result of what? of rrr? Commented Aug 31, 2013 at 23:47

2 Answers 2

1

$w is an array , not a value , so you try to pass array in your query , it should be something like that :

$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
Sign up to request clarification or add additional context in comments.

Comments

1

In post2.php you are attempting to use an array in a database query. Try:

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.