显示数据库中的复选框以进行编辑


Showing checked checkbox from database for editing

我正在尝试显示数据库中选中的复选框,这些复选框是在插入用户时添加的。但当我编辑该用户时,我希望将这些复选框显示为已选中。如何做到这一点。这是我的编辑代码。

$edit = true;
$id = $_REQUEST['id'];
$userEdit = $executeQuery->oneRowCondition("user_master", "user_id='$id'");
if(isset($_REQUEST['subMitBtn'])){
    @$username = $_REQUEST['user_name'];
    @$compID = $_REQUEST['compID'];
    @$email=$_REQUEST['email'];
    @$password=$_REQUEST['password'];
    @$q1=explode(',',$_POST['q1']);
    @$q2=$q1.",";

    $insertUser = mysqli_query($conn, "UPDATE user_master SET 
         user_name = '$username', user_email='$email', user_password='$password',user_module='$q2' where  user_id = '$compID' ");
    echo "UPDATE user_master SET 
         user_name = '$username', user_email='$email', user_password='$password',user_module='$q2' where  user_id = '$compID' ";     

}

HTML代码是:

<label class="col-sm-2 control-label">Modules</label>
<div class="col-sm-3">
<input type="checkbox" id="q1[]" name="q1[]" value="Dashboard" > Dashboard<br>
<input type="checkbox" id="q1[]" name="q1[]" value="Order Entry"> Order Entry <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Status"> Status <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Customers"> Customers <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Shipping Company" > Shipping Company <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Shipping Rates" > Shipping Rates <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Retailer"> Retailer <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Marketing"> Marketing <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Sales Report"> Sales Report <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Inward Report"> Inward Report <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Outward Report"> Outward Report <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Users & Roles"> Users & Roles <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Change Password"> Change Password <br>
<input type="checkbox" id="q1[]" name="q1[]" value="Email Settings"> Email Settings <br>

首先,@$q1=explode(',',$_POST['q1']);是不正确的。我敢肯定,您正在尝试在这里执行implode,以获得数组的字符串表示。应该是q1 = implode(',', $_POST['q1'])

第二,你的问题,

当我编辑该用户时,我想将这些复选框显示为选中的

从数据库中获取所有复选框值,如下所示:

$result_set = mysqli_query($conn, "SELECT user_module FROM user_master WHERE user_id = '$compID'");
$result = mysqli_fetch_assoc($result_set);
$checkbox_array = explode(",", $result['user_module']);

你的HTML应该是这样的:

// your code
<input type="checkbox" id="q1[]" name="q1[]" value="Dashboard"<?php if(in_array("Dashboard", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Dashboard<br />
<input type="checkbox" id="q1[]" name="q1[]" value="Order Entry"<?php if(in_array("Order Entry", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Order Entry <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Status"<?php if(in_array("Status", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Status <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Customers"<?php if(in_array("Customers", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Customers <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Shipping Company"<?php if(in_array("Shipping Company", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Shipping Company <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Shipping Rates"<?php if(in_array("Shipping Rates", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Shipping Rates <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Retailer"<?php if(in_array("Retailer", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Retailer <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Marketing"<?php if(in_array("Marketing", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Marketing <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Sales Report"<?php if(in_array("Sales Report", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Sales Report <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Inward Report"<?php if(in_array("Inward Report", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Inward Report <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Outward Report"<?php if(in_array("Outward Report", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Outward Report <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Users & Roles"<?php if(in_array("Users & Roles", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Users & Roles <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Change Password"<?php if(in_array("Change Password", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Change Password <br />
<input type="checkbox" id="q1[]" name="q1[]" value="Email Settings"<?php if(in_array("Email Settings", $checkbox_array)){ echo " checked='"checked'""; } ?> /> Email Settings <br />
// your code

有几种方法可以做到这一点,但其中一种方法的基本原理是:

<input type="checkbox" id="mycheckbox" name="mycheckbox" value="Inward Report"
<?php if($mycheckbox==true) echo " checked "?>
>