如何从PHP中的数据库中检索复选框值?

时间:2016-11-24 08:51:46

标签: php

这是我的代码

<tr><td valign="top">Sebab Kekosongan</td>
      <?php
            $QQQ = "SELECT sbbKekosongan FROM infojawatan WHERE ID = '$ID'";
            $rs_QQQ = mysql_query($QQQ);
            while($row_QQQ = mysql_fetch_array($rs_QQQ))
            {
                $kerana = explode(",", $row_QQQ['kerana']); ?>  
     <td>           
    <input type="checkbox" name="kerana[]" value="retire" <?php if(in_array("retire",$kerana)) echo 'checked="checked"' ?>> Bersara

&gt; Meninggal Dunia         ##继续编码##      
        &GT; Bertukar

    <input type="checkbox" name="kerana[]" value="promote" <?php if(in_array("promote",$kerana)) echo 'checked="checked"' ?>> Naik Pangkat

    <input type="checkbox" name="kerana[]" value="others"  <?php if(in_array("others",$kerana)) echo 'checked="checked"' ?>> Lain -lain

      </td>  <?php } ?>  </tr> 

我的第二个测试代码

 <tr><td valign="top">Sebab Kekosongan</td><td>
        <?php 
            $keranaS = array('retire', 'death', 'change', 'promote', 'others');
            if(! empty($keranaS))
            {
                foreach ($keranaS as $myKerana)
                {
                    $checked = (in_array($myKerana, $kerana)) ? 'checked="checked"' : ''; ?>

                   <input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>" <?php echo $checked; ?>> <?php echo $myKerana;?>
          <?php } ?>
          </td>
    <?php   } ?>

    </tr>

我试过了两个。但是它不起作用

1 个答案:

答案 0 :(得分:-1)

        # Please Try again below code.. #


        <tr><td valign="top">Sebab Kekosongan</td><td>
                <?php 
                    $keranaS = array('retire', 'death', 'change', 'promote', 'others');
                    //echo '<pre>'; print_r($keranaS); exit;
                    if(!empty($keranaS))
                    {
                        foreach ($keranaS as $myKerana)
                        {
                            $checked = (in_array($myKerana, $keranaS)) ? 'checked="checked"' : ''; ?>

                           <input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>" <?php echo $checked; ?>> <?php echo $myKerana;?>
                  <?php } ?>
                  </td>
            <?php   } ?>

            </tr>



    # Please try again below dynamic through checkbox checked.  


<tr><td valign="top">Sebab Kekosongan</td><td>
    <?php 
        $keranaS = array('retire', 'death', 'change', 'promote', 'others');

        //$kerana_data data databse though fetch
        $kerana_data = array('retire', 'change', 'others');

        if(!empty($keranaS))
        {
            $i = 0;
            foreach ($keranaS as $myKerana)
            { 
                if(in_array($myKerana, $kerana_data)){
                    $checked = (in_array($myKerana, $kerana_data)) ? 'checked="checked"' : ''; ?>
                    <input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>" <?php echo $checked; ?>> <?php echo $myKerana;?>
                <?php }else{?>
                    <input type="checkbox" name="kerana[]" value="<?php echo $myKerana; ?>"> <?php echo $myKerana;?>
                <?php }?> 
      <?php $i++;
            } ?>
      </td>
<?php   } ?>
</tr>