如果数据库表中存在值,请选中复选框


Check a checkbox if value is present in a db table

我正在用从数据库表中获得的数据填充复选框。我的问题与SO上的其他问题不同,因为我定义了一个复选框,当从数据库填充时,它会循环。因此,我想选中包含另一个表中存在的值的复选框。使用我的代码,只选中了包含表最后一行值的复选框。我做错了什么?

    //this is the value I get from the other table.
    $indb = "$bid,$c,$sc,$p";
    $q5=mysqli_query($link,"SELECT * FROM brands_offer WHERE Brand_Id='$bid' AND Published='1' ");
    while($row5 = mysqli_fetch_array($q5)){
        $catid= $row5['Catg_Id'];
        $subcatid= $row5['Subcatg_Id'];
        $pid= $row5['Product_Id'];      
        //this is the value that populates the checkboxes
        $vals="$bid,$catid,$subcatid,$pid";
    ?>
    //my argument is here
    <input type="checkbox" name="checkbox[]" value="<?php echo "$vals";?>" <?php if ($vals == $indb){?> checked="checked" <?php } ?>>
    <?php
        echo $bname;
        echo " ->  ";
        echo $catid;
        echo ",  ";
        echo $subcatid;
        echo ",  ";
        echo $pid;
        echo "  ";
    }

得到它后,我将表中的值添加到数组$indb[] = "$bid,$c,$sc,$p";中,并使用<?php if (in_array($vals, $indb)){?> checked="checked" <?php } ?>检查它是否在数组中。工作:D