检查数据库值php的复选框

时间:2015-12-03 13:14:31

标签: php

我有以下代码。我想根据数据库值检查复选框。当我在一段时间内编写代码时,我按下按钮组多次,因为我检查了按钮,但无论如何我都选中了正确的按钮。当代码出现时,我得到警告:in_array()期望参数2为数组,给定为。我该怎么办?

 <?php
   $query=" select description from doors
            where id_doors IN
            (select doors_id_doors from access_rights 
            where users_rfidcode='".$result['rfidcode']."'); ";

   $resultt=$conn->query($query);
       while($row=$resultt->fetch_assoc()){}
            //in_array ()check if value is in array 
               if(in_array("SW1",$row))echo '<input type="checkbox" name="chk[]" value="SW1" checked >SW1'; else echo '<input type="checkbox" name="chk[]" value="SW1"  >SW1';
               if(in_array("SW2",$row))echo '<input type="checkbox" name="chk[]" value="SW2" checked >SW2';  else echo '<input type="checkbox" name="chk[]" value="SW2"  >SW2';
               if(in_array("SW3",$row))echo '<input type="checkbox" name="chk[]" value="SW3" checked >SW3';  else echo '<input type="checkbox" name="chk[]" value="SW3"  >SW3';
               if(in_array("PR1",$row))echo '<input type="checkbox" name="chk[]" value="PR1" checked >PR1';  else echo '<input type="checkbox" name="chk[]" value="PR1"  >PR1';
               if(in_array("PR2",$row))echo '<input type="checkbox" name="chk[]" value="PR2" checked >PR2';  else echo '<input type="checkbox" name="chk[]" value="PR2"  >PR2';
   ?>

1 个答案:

答案 0 :(得分:0)

你关闭了括号然后你写了html的代码,所以试试这个:

<?php
   $query=" select description from doors
            where id_doors IN
            (select doors_id_doors from access_rights 
            where users_rfidcode='".$result['rfidcode']."'); ";

   $resultt=$conn->query($query);
       while($row=$resultt->fetch_assoc()){
            //in_array ()check if value is in array 
               if($row['description'] =="SW1") echo '<input type="checkbox" name="chk[]" value="SW1" checked >SW1'; else echo '<input type="checkbox" name="chk[]" value="SW1"  >SW1';
               if($row['description'] =="SW2") echo '<input type="checkbox" name="chk[]" value="SW2" checked >SW2';  else echo '<input type="checkbox" name="chk[]" value="SW2"  >SW2';
               if($row['description'] =="SW3") echo '<input type="checkbox" name="chk[]" value="SW3" checked >SW3';  else echo '<input type="checkbox" name="chk[]" value="SW3"  >SW3';
               if($row['description'] =="PR1") echo '<input type="checkbox" name="chk[]" value="PR1" checked >PR1';  else echo '<input type="checkbox" name="chk[]" value="PR1"  >PR1';
               if($row['description'] =="PR2")  echo '<input type="checkbox" name="chk[]" value="PR2" checked >PR2';  else echo '<input type="checkbox" name="chk[]" value="PR2"  >PR2';
}
   ?>

您也可以将代码改进为更具可读性:

    <?php
   $query=" select description from doors
            where id_doors IN
            (select doors_id_doors from access_rights 
            where users_rfidcode='".$result['rfidcode']."'); ";

   $resultt=$conn->query($query);
       while($row=$resultt->fetch_assoc()){
            //in_array ()check if value is in array 

                $SW1_checked='';
                $SW2_checked='';
                $SW3_checked='';
                $PR1_checked='';
                $PR2_checked='';
               if($row['description'] =="SW1") {$SW1_checked='checked';} 
               elseif($row['description'] =="SW2") {$SW2_checked='checked';}
               elseif($row['description'] =="SW3") {$SW3_checked='checked';}
               elseif($row['description'] =="PR1") {$PR1_checked='checked';}
               elseif($row['description'] =="PR2") {$PR2_checked='checked';}


}
             echo '<input type="checkbox" name="chk[]" value="SW1" '.$SW1_checked.' >SW1'; 
            echo '<input type="checkbox" name="chk[]" value="SW2"  '.$SW2_checked.' >SW2';  
            echo '<input type="checkbox" name="chk[]" value="SW3"  '.$SW3_checked.' >SW3';  
             echo '<input type="checkbox" name="chk[]" value="PR1"  '.$PR1_checked.' >PR1';  
             echo '<input type="checkbox" name="chk[]" value="PR2"  '.$PR2_checked.' >PR2';  
   ?>