jchan jchan - 5 months ago 13
PHP Question

HTML input checkbox get value from mysql via PHP

How do I set the checkbox

nd
is checked if the value in
mysql
is
1
?

<?php
$select_PO=mysql_query("SELECT pono, nd, r, adj, no, am, ct, poyes, poyesp, pon FROM tb_po WHERE pono = '". $id ."' AND postatus !='Approved' AND pono NOT IN (SELECT pono FROM tb_postatus)", $conn);
$count=mysql_num_rows($select_PO);

if ($count == 1) {
$row=mysql_fetch_array($select_PO);
if ($row['nd'] = 1) {
$nd="checked";
}
if ($row['r'] = 1) {
$nd="checked";
}
mysql_close($conn);
?>
<input type="checkbox" id="nd" name="nd" checked="<?php echo $nd ?> "/>

Answer

Check this out,

<?php
    $select_PO=mysql_query("SELECT pono, nd, r, adj, no, am, ct, poyes, poyesp, pon FROM tb_po WHERE pono = '". $id ."' AND postatus !='Approved' AND pono NOT IN (SELECT pono FROM tb_postatus)", $conn);
    $count=mysql_num_rows($select_PO);

    if ($count == 1) {
        $row=mysql_fetch_array($select_PO);
        if ($row['nd'] = 1) {
            $nd="checked";
        }
        if ($row['r'] = 1) {
            $nd="checked";
        }
        mysql_close($conn);
?>
<input type="checkbox" id="nd" name="nd" <?php echo $nd ?> />