使用PHP&更新复选框值MySQL的

时间:2014-08-11 07:27:50

标签: php mysql

表:

role_id   role_name              perm_id
  1         admin                  0
  2         Manager                0
  7         Accounts Assitant      4,6
  8         Registrar              2,5,6

我从PHP表格中存储所有这些信息

<form action="" method="post">               
   <table>
    <tr><th>ROLE</th>
        <th>PERMISSIONS<br><input type="checkbox" class="chk_boxes"></th>  
    </tr>
    <tr>
      <td><input type="text" name="role_name" required></td>      
      <td><input type="checkbox" class="chk_boxes1" name="perm[]" value="1">My Account<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="2">Edit Account<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="3">Change password<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="4">List of users<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="5">Define roles<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="6">Assign roles<br>
      </td>
    </tr></table>
    <div><input type="submit" name="submit" value="create role"></div>
  </form>

我成功地将数据提交到数据库,从数据库中检索复选框值时遇到了问题。   现在我想编辑这个表单并显示复选框(选择一个并取消选择一个)。

<?php
    $query = $db->prepare("SELECT * FROM role WHERE role_id = ".$role_id." ");
    $query->execute();
    foreach($query as $q)
    {
      echo '<form action="" method="post">               
             <table>
              <tr><th>ROLE</th>
                  <th>PERMISSIONS<br><input type="checkbox" class="chk_boxes"></th>  
              </tr>
              <tr><td><input type="text" value="'.$q['role_name'].'" name="role_name" required></td>
                  <td>';
                      $permid_array = $q['perm_id'];                                          
                      foreach(explode(',', $permid_array) as $n)
                      {
                        if ($n == 1 || $n == 2 || $n == 3 || $n == 4 || $n == 5 || || $n == 6) { $set_checked = "checked";}
                        else {$set_checked = ""; }

                        echo '
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="1" '.$set_checked.' > My Account<br> 
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="2" '.$set_checked.' > Edit Account<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="3" '.$set_checked.' > Change Password<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="4" '.$set_checked.' > List of users<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="5" '.$set_checked.' > Define roles<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="6" '.$set_checked.' > Assign roles<br>';
                      }
                   echo '</td></tr>
             </table>
             <div><input type="submit" name="submit" value="create role"></div>
            </form>';
    }
  ?>

当我执行编辑表单时,复选框将重复出现。建议我在哪里出错或指导我做其他方式。

非常感谢

1 个答案:

答案 0 :(得分:2)

你这样做:

echo '
<input type="checkbox" class="chk_boxes1" name="perm[]" value="1" '.$set_checked.' > My Account<br> 
<input type="checkbox" class="chk_boxes1" name="perm[]" value="2" '.$set_checked.' > Edit Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="3" '.$set_checked.' > Change Password<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="4" '.$set_checked.' > List of users<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="5" '.$set_checked.' > Define roles<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="6" '.$set_checked.' > Assign roles<br>';
每个perm_id

。如果你的perm_id是2,4,那么你会做两次。如果你有2,你将只做一次,如果你有1,2,3,4,5,6,你将做6次。

您可以通过循环遍历每个值来轻松解决它:

$arr = array(1,2,3,4,5,6);
$arrNames = array('My Account', 'Edit Account', 'Change Password', 'List of users', 'Define roles', 'Assign Roles');
foreach($arr as $val) {
    $set_checked = "";
    if(in_array($val, $permid_array)) {
        $set_checked = "checked";
    }
    echo '<input type="checkbox" class="chk_boxes1" name="perm[]" value="$val" '.$set_checked.' > '.$arrNames[$val].' <br>'

    }
}

如果您想坚持原始解决方案,那么您需要更改:

 $permid_array = explode(',', $q['perm_id']);                                          
 echo '
<input type="checkbox" class="chk_boxes1" name="perm[]" value="1" '.(in_array(1, $permid_array))?'checked':''.' > My Account<br> 
<input type="checkbox" class="chk_boxes1" name="perm[]" value="2" '.(in_array(2, $permid_array))?'checked':''.' > Edit Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="3" '.(in_array(3, $permid_array))?'checked':''.' > Change Password<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="4" '.(in_array(4, $permid_array))?'checked':''.' > List of users<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="5" '.(in_array(5, $permid_array))?'checked':''.' > Define roles<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="6" '.(in_array(6, $permid_array))?'checked':''.' > Assign roles<br>';