从表中的数据库中选取复选框并使用PHP将其更新回来

时间:2013-11-20 08:19:48

标签: php mysql

我从数据库中选择数据并在表格中排列它还包含一个复选框,如果为1则为true,如果为0则为false。程序应根据复选框功能更新数据库。

        <?php
            $query1="SELECT * FROM notices;";
            $result1=mysql_query($query1,$conn);
            $num_rows=mysql_num_rows($result1);
            $pick=array();
            if($num_rows>0)
            {
                while ( ($row=mysql_fetch_array($result1)) != null)
                { 
                $pick=$row['sl'];
                ?>
                <tr class="trc">
                    <td align="left"><label class="lab1"><?php echo($row['notice']); ?></label></td>
                    <td align="center"><label class="lab1"><?php echo($row['date']); ?></label></td>
                    <td align="center"><input type="checkbox" name="notice_id[]" value="YES" <?php 
                                            if($row['active']==1)
                                            {
                                                echo("checked");
                                            }
                                            ?> /></td>
                </tr>
            <?php 
                }
            }
        ?>

以下是将数据更新回数据库的代码。

if(isset($_POST['up_note'])==true)
{
    $n_ids = $_POST['notice_id'];
    $sl_no = 1;

    foreach($n_ids as $key => $id)
    {
        echo($n_ids." | ".$id." | ".$key."<br />");

        $cb = ($POST['notice_id[$id]'] == 'YES')?'1':'0';

        if($cb==1)
        {
            $query3 = "UPDATE notices SET active = 1 WHERE sl='$sl_no';";
            $result3 = mysql_query($query3, $conn);
            echo("entered");
        }
        else
        {
            $query3 = "UPDATE notices SET active = 0 WHERE sl='$sl_no';";
            $result3 = mysql_query($query3, $conn);
            echo("not entered");
        }
        $sl_no = $sl_no + 1;    
    }
}

2 个答案:

答案 0 :(得分:0)

您确定这一行吗

$cb = ($POST['notice_id[$id]'] == 'YES')?'1':'0';

我认为它打算成为

$cb = ($_POST['notice_id[$id]'] == 'YES')?'1':'0';

并将双引号替换为单引号

$cb = ($_POST["notice_id[$id]"] == 'YES')?'1':'0';

答案 1 :(得分:0)

<?php
            $query1="SELECT * FROM notices;";
            $result1=mysql_query($query1,$conn);
            $num_rows=mysql_num_rows($result1);
            $pick=array();
            if($num_rows>0)
            {
                while ( ($row=mysql_fetch_array($result1)) != null)
                { 
                $pick=$row['sl'];
?>
                <tr class="trc">
                    <td align="left"><label class="lab1"><?php echo($row['notice']); ?></label></td>
                    <td align="center"><label class="lab1"><?php echo($row['date']); ?></label></td>
                    <td align="center"><input type="checkbox" name="notice_id[]" value="<?php echo $pick; ?>" <?php if($row['active']==1){ echo("checked"); } ?> /></td>
                </tr>
<?php 
                }
            }
?>


/////////////////////////////////////
if(isset($_POST['up_note'])==true)
{
    if(!empty($_POST['notice_id'])){
        $n_ids = $_POST['notice_id'];
            $query_all = "UPDATE notices SET active = 0";
            $result_all = mysql_query($query_all, $conn);
        foreach($n_ids as $key => $id){
            $sl_no = $n_ids[$key];
            $query3 = "UPDATE notices SET active = 1 WHERE sl=$sl_no;";
            $result3 = mysql_query($query3, $conn);
            echo("entered");    
        }
    } else {
           $query_all = "UPDATE notices SET active = 0";
           $result_all = mysql_query($query_all, $conn);
    }
}