mercredi 22 juillet 2015

Why I can't change data in database by unchecking checkboxes in CodeIgniter?

So I get this simple forum project from this website:

CIBB Basic Forum with Codeigniter

But I have problem solving this one problem with checkbox. The problem came with the original code from the website. And I can't solve the problem for almost 2 days now.

The problem is really simple, when I want to add privileges to some role (administrator, editor, etc) I can check some checkboxes to do that. It works just fine if I want to add privileges, but I can't remove every single privilege I already added to the role. Here's the code:

Controller

 public function role_edit($role_id)
{
    if ($this->session->userdata('role_edit') == 0) {
        redirect('admin');
    }
    if ($this->input->post('btn-edit')) {
        $this->admin_model->role_edit();
        if ($this->admin_model->error_count != 0) {                
            $this->data['error'] = $this->admin_model->error;
        } else {
            $this->session->set_userdata('tmp_success', 1);
            redirect('admin/role_edit/'.$role_id);
        }
    }
    $tmp_success = $this->session->userdata('tmp_success');
    if ($tmp_success != NULL) {
        // role updated
        $this->session->unset_userdata('tmp_success');
        $this->data['tmp_success'] = 1;
    }
    $this->data['role'] = $this->db->get_where(TBL_ROLES, array('id' => $role_id))->row();
    $this->data['title']   = 'Admin Role Edit :: '.CIBB_TITLE;
    $this->load->view('header', $this->data);
    $this->load->view('admin/sidebar');
    $this->load->view('admin/role_edit');
    $this->load->view('footer');
}

Model

public function role_edit()
{
    $row = $this->input->post('row');

    // check role name
    if (strlen($row['role']) == 0) {
        $this->error['role'] = 'Role name cannot be empty';
    } else {
        if ($row['role'] != $row['role_c']) {
            $role_check = $this->db->get_where(TBL_ROLES, array('role' => $row['role']));
            if ($role_check->num_rows() > 0) {
                $this->error['role'] = 'Role name "'.$row['role'].'" already in use';
            }
        }
    }

    // check roles
    if (!isset($row['roles'])) {
        $this->error['roles'] = 'Choose minimum 1 role';
    }

    if (count($this->error) == 0) {
        unset($row['role_c']);

        // reset row value
        $row_reset = $row['roles'];
        foreach ($row_reset as $key => $value) {
            $row_reset[$key] = 0;
        }

        $this->db->where('id', $row['id']);
        $this->db->update(TBL_ROLES, $row_reset);

        // update role
        $data = array();
        $data['role'] = $row['role'];
        foreach ($row['roles'] as $key => $value) {
            $data[$key] = 1;
        }
        $this->db->where('id', $row['id']);
        $this->db->update(TBL_ROLES, $data);
    } else {
        $this->error_count = count($this->error);
    }
}

Views

<div class="span10">  
<div class="page-header">
    <h1>Edit Role</h1>
</div>
<form class="form-horizontal" action="" method="post">
    <?php if (isset($tmp_success)): ?>
    <div class="alert alert-success">
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <h4 class="alert-heading">Role updated!</h4>
    </div>
    <?php endif; ?>

    <?php if (isset($error)): ?>
    <div class="alert alert-error">
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <h4 class="alert-heading">Error!</h4>
        <?php if (isset($error['role'])): ?>
            <div>- <?php echo $error['role']; ?></div>
        <?php endif; ?>
        <?php if (isset($error['roles'])): ?>
            <div>- <?php echo $error['roles']; ?></div>
        <?php endif; ?>

    </div>
    <?php endif; ?>
    <fieldset>
      <input type="hidden" name="row[id]" value="<?php echo $role->id; ?>"/>
      <input type="hidden" name="row[role_c]" value="<?php echo $role->role; ?>"/>
      <div class="control-group">
        <label class="control-label" for="input01">Role Name</label>
        <div class="controls">
          <input class="input-xlarge" id="role-name" value="<?php echo $role->role; ?>" name="row[role]" type="text">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="input01"></label>
        <div class="controls">
            <p class="help-block"><strong>Note:</strong> pick at least one role function below</p>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="optionsCheckbox"><b>Admin</b></label>
        <div class="controls">
          <label class="checkbox inline">
            <input id="cb_thread_create" name="row[roles][admin_area]" <?php if ( $role->admin_area == 1 ): ?>checked<?php endif; ?> type="checkbox"> access area 
          </label>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="optionsCheckbox"><b>Thread</b></label>
        <div class="controls">
          <label class="checkbox inline">
            <input id="cb_thread_create" name="row[roles][thread_create]" <?php if ( $role->thread_create == 1 ): ?>checked<?php endif; ?> type="checkbox"> create 
          </label>
          <label class="checkbox inline">
            <input id="cb_thread_edit" name="row[roles][thread_edit]" <?php if ( $role->thread_edit == 1 ): ?>checked<?php endif; ?> type="checkbox"> edit 
          </label>
          <label class="checkbox inline">
            <input id="cb_thread_delete" name="row[roles][thread_delete]" <?php if ( $role->thread_delete == 1 ): ?>checked<?php endif; ?> type="checkbox"> delete 
          </label>
        </div>
      </div>

      <div class="control-group">
        <label class="control-label" for="optionsCheckbox"><b>Post</b></label>
        <div class="controls">
          <label class="checkbox inline">
            <input id="cb_post_create" name="row[roles][post_create]" <?php if ( $role->post_create == 1 ): ?>checked<?php endif; ?> type="checkbox"> create 
          </label>
          <label class="checkbox inline">
            <input id="cb_post_edit" name="row[roles][post_edit]" <?php if ( $role->post_edit == 1 ): ?>checked<?php endif; ?> type="checkbox"> edit 
          </label>
          <label class="checkbox inline">
            <input id="cb_post_delete" name="row[roles][post_delete]" <?php if ( $role->post_delete == 1 ): ?>checked<?php endif; ?> type="checkbox"> delete 
          </label>
        </div>
      </div>

      <div class="control-group">
        <label class="control-label" for="optionsCheckbox"><b>Role</b></label>
        <div class="controls">
          <label class="checkbox inline">
            <input id="cb_post_create" name="row[roles][role_create]" <?php if ( $role->role_create == 1 ): ?>checked<?php endif; ?> type="checkbox"> create 
          </label>
          <label class="checkbox inline">
            <input id="cb_post_edit" name="row[roles][role_edit]" <?php if ( $role->role_edit == 1 ): ?>checked<?php endif; ?> type="checkbox"> edit 
          </label>
            <label class="checkbox inline">
            <input id="cb_post_delete" name="row[roles][role_delete]" <?php if ( $role->role_delete == 1 ): ?>checked<?php endif; ?> type="checkbox"> delete 
          </label>
      </div>

      <div class="form-actions">
        <input type="submit" name="btn-edit" class="btn btn-primary" value="Save Role"/>
      </div>
    </fieldset>
  </form>

My questions:

  1. Why unchecking checkboxes wouldn't change the data in database?
  2. I read an article about similar problem, based on this article I can solve this problem with Ajax, is that true? How?
  3. If I can add but can't remove it, I suspect that maybe the original coder just wrote wrong update statement, am I right? Why?



Aucun commentaire:

Enregistrer un commentaire