Php 显示数据库中的复选框以进行编辑
我试图显示在插入用户时添加的数据库中的复选框。但当我编辑该用户时,我希望将这些复选框显示为选中。如何操作。这是我的编辑代码Php 显示数据库中的复选框以进行编辑,php,html,checkbox,Php,Html,Checkbox,我试图显示在插入用户时添加的数据库中的复选框。但当我编辑该用户时,我希望将这些复选框显示为选中。如何操作。这是我的编辑代码 $edit = true; $id = $_REQUEST['id']; $userEdit = $executeQuery->oneRowCondition("user_master", "user_id='$id'"); if(isset($_REQUEST['subMitBtn'])){ @$username = $_REQUEST['user_n
$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>
模块
仪表板
订单输入
状态
客户
航运公司
运费
零售商
营销
销售报告
内部报告
对外报告
用户和角色
更改密码
电子邮件设置
有几种方法可以做到这一点,但其中一种方法的基本原则是:
<input type="checkbox" id="mycheckbox" name="mycheckbox" value="Inward Report"
<?php if($mycheckbox==true) echo " checked "?>
>
首先,@$q1=explode(“,”,$”POST['q1'])代码>不正确。我很确定您在这里尝试进行内爆
,以获得数组的字符串表示形式。它应该是q1=内爆(',',$\u 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']);
// 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
您的HTML应该如下所示:
$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']);
// 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
//您的代码
/>订单输入
/>客户
/>运费
/>营销
/>内部报告
/>用户和角色
/>电子邮件设置
//你的代码
你确定@$q1=explode(',',$\u POST['q1'])代码>是否正确?难道你不想做内爆()
?而是为了解决你的问题。看起来您正在将复选框保存为csv。您需要执行一个explode()
,然后可以对每个值使用in_array()
,查看它是否在数组中,如果在数组中,请将其设置为选中。谢谢您帮助我这么做。@sneha很高兴我能提供帮助。:)