使用PHP更新复选框值MySQL


Update Checkbox Value Using PHP & MySQL

table:

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>';
    }
  ?>

当我执行编辑表单时,复选框重复出现。建议我哪里做错了,或者指导我怎么做。

多谢

你可以:

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>';