title: Update selected check box data and remove unchecked data
body: <p>Assume I have <strong>users</strong> database and <code>base_u_group</code> default will be 0 which are not under any groups.</p>
  
  <pre>
  1)base_users
  |base_u_id|base_u_username|base_u_group|
  ------------------------------------------
  |    1    |   username 1  |      0     |
  |    2    |   username 2  |  2, 3, 4   |
  |    3    |   username 3  |     4      |
  |    4    |   username 4  |    3,5     |
  </pre>
  
  <p>List down all the users. <code>$checkBox</code> will be automatically checked when belong to that edit group.</p>
  
  <pre><code>echo "&lt;tr&gt;";
  echo   "&lt;td&gt;". $count .". ".$row_User['base_u_username']. "&lt;/td&gt;";
  echo   "&lt;td align=\"center\"&gt;&lt;input type=\"checkbox\" class = \"group\" name=\"userList[]\" value=".$row_User['base_u_id']." ".$checkBox."/&gt;&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  </code></pre>
  
  <p>My problem is how do I insert selected check boxes data without duplication for example: <br><code>3, 3, 4, 5 --&gt; 3, 4, 5</code> <br>
  and when the check boxes are unchecked, it will delete that group in my <code>base_u_group</code> , for example unchecked the check box for <strong>username 2</strong> of <strong>group 3</strong><br></p>
  
  <pre><code>2, 3, 4 --&gt; 2, 4
  </code></pre>
  
  <p><br>
  <code>$gid</code> indicates the selected edit group.
  This is what I did so far:</p>
  
  <pre><code>&lt;?php
  $userGroup = $_POST['userList'];
  
  foreach($userGroup as $a)
  {   
      $selSQL = base_executeSQL("SELECT * FROM base_users WHERE base_u_id='".$a."'");
      while($row_SQL = base_fetch_array($selSQL))
      if($row_SQL['base_u_group'] != "0")
      {           
          $data = explode(", ",$row_SQL['base_u_group']);
          for($i=0; $i&lt;count($data);$i++)
          {
              //insert to user group if base_u_group does not find the group ID
              if($gid &lt;&gt; $data[$i])
                  base_executeSQL("UPDATE base_users SET base_u_group='".$gid. ", ". $row_SQL['base_u_group']."' WHERE base_u_id='".$a."'");
          }
      }   
      //if the user does not belong to any groups: base_u_group = 0
      else
          base_executeSQL("UPDATE base_users SET base_u_group='".$gid."' WHERE base_u_id='".$a."'");
  }
  ?&gt;
  </code></pre>
  
  <p>EDIT: deletion of groups</p>
  
  <pre><code>$data = explode(", ",$row_SQL['base_u_group']);
  $ok = true;
  for($i=0; $i&lt;count($data);$i++)
  {
      //insert to user group if base_u_group does not find the group ID
      if($gid == $data[$i])
      {
          $arr = array_merge(array_diff($data,array($gid)));
          $newArray = implode(", ",$arr);     
          base_executeSQL("UPDATE base_users SET base_u_group='".$newArray."' WHERE base_u_id!='".$row_SQL['base_u_id']."' AND base_u_domain='local'");
          $ok = false;
      }   
  }
  if (ok) base_executeSQL("UPDATE base_users SET base_u_group='".$gid. ", ". $row_SQL['base_u_group']."' WHERE base_u_id='".$a."'");
  </code></pre>