我有关于用户角色的ranks
表
---------------------------------------------------------------------------
| rank | areas | intranet_view | intranet_writer | admin_users |
---------------------------------------------------------------------------
| 1 | 'AREA I' | 1 | 0 | 0 |
| 2 | 'AREA VI' | 1 | 1 | 0 |
| ... | ... | ... | ... | ... |
---------------------------------------------------------------------------
我曾经像这样更新它
// form for selecting allowed areas
echo "<select size=\"10\" class=\"form-control\" name=\"areas[]\" multiple>";
$kysely = $pdo->query("SELECT * FROM customer_areas");
while($areas = $kysely->fetch(PDO::FETCH_ASSOC)) {
$areaArray = explode(',',$result['areas']);
echo "<option value=\"".$areas['customer_area_code']."\""; if(in_array($areas['customer_area_code'], $areaArray)){ echo " selected"; } echo ">".$areas['customer_area_name']."</option>";
}
echo "</select>";
// and this form is for selecting allowed privileges
$kysely = $pdo->query("SELECT * FROM ranks");
$rights = $kysely->fetch(PDO::FETCH_ASSOC);
foreach($rights as $key => $value) {
if($key == 'rank' || $key == 'areas') {
} else {
echo "<div class=\"checkbox\"><label><input type=\"hidden\" name=\"".$key."\" value=\"0\" /><input type=\"checkbox\" name=\"".$key."\" value=\"1\""; if($result[$key] == '1'){ echo " checked"; } echo ">".mb_strtolower($key)."</label></div>";
}
}
// following is the form action page
if (isset($_POST['areas'])) {
$areas = implode(",", $_POST['areas']);
} else {
$areas = "";
}
try {
$pdo->beginTransaction();
$kysely = $pdo->query("SELECT * FROM ranks");
$rights = $kysely->fetch(PDO::FETCH_ASSOC);
$i = 0;
$sql = "";
foreach ($rights as $key => $value) {
if ($key == 'rank' || $key == 'areas') {
} else {
if ($i != 0) {
$sql .= ", ";
}
$sql .= $key . " = '" . $_POST[$key] . "'";
$i++;
}
}
$kysely = $pdo->prepare("UPDATE ranks SET areas = ?, $sql WHERE rank = ?");
$kysely->execute(array($areas, $id));
$pdo->commit();
} catch (PDOException $e) {
$pdo->rollback();
}
我想知道是否有一些好方法可以在多个选择中拥有这些权限。当我创建这个代码时,我的智能不足以进行多重选择,所以我制作了复选框。
问题是我不知道如何将列名放到多个选择中,以便更新正常工作。我是否需要添加例如name=\"option".[$key]."\"
或其他什么。我对如何做到这一点一无所知。
提前致谢。
答案 0 :(得分:-1)
是的,您可以按名称name=\"option".[$key]."\"
检索
像
<?php
foreach($options $key => $value):
echo '<input type="checkbox" name="option['.$key.']" value="$vlaue">';
endforeach;
?>
所以它看起来像这样,
<input type="checkbox" name="option[0]" value="some_value0">
<input type="checkbox" name="option[1]" value="some_value1">
<input type="checkbox" name="option[2]" value="some_value2">