• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If All User Group Unchecked Not Updating Database

#1
[eluser]riwakawd[/eluser]
Hi,

I have a user group permission role form.

I am having a slight problem.

When I unselect all my permissions and the click on submit it does not let me clear the permissions from that user group id. and does not redirect back to admin/users_group.

If click all unselect it and then click submit it should clear those permissions from that user group id.

Code:
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE user_group_id = '10'' at line 4
UPDATE user_group SET name = 'Demonstration', permission = WHERE user_group_id = '10'
Filename: C:\Xampp\htdocs\codeigniter-project\system\database\DB_driver.php
Line Number: 330

Model edit function

Code:
public function editUserGroup($user_group_id, $data) {
$this->db->query("UPDATE " . $this->db->dbprefix . "user_group SET
name = " . $this->db->escape($data['name']) . ",
permission = " . (isset($data['permission']) ? $this->db->escape(serialize($data['permission'])) : '') . "
WHERE
user_group_id = '" . (int)$user_group_id . "'
");
}

Controller edit function

Code:
public function edit() {
$this->load->model('admin/user/users_group_model');

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {

$user_group_id = $this->uri->segment(4);

$this->users_group_model->editUserGroup($user_group_id, $this->request->post);

$this->session->set_flashdata('success', $this->lang->line('text_success'));

redirect('admin/users_group');

}

$this->getForm();
}

#2
[eluser]CroNiX[/eluser]
Totally standard HTML form behavior. Unchecked boxes do not get transmitted with form data, so you need to check to see if it was posted and if it wasn't, then no boxes were checked and take appropriate action.

#3
[eluser]riwakawd[/eluser]
[quote author="CroNiX" date="1413565435"]Totally standard HTML form behavior. Unchecked boxes do not get transmitted with form data, so you need to check to see if it was posted and if it wasn't, then no boxes were checked and take appropriate action.[/quote]

On my view this is what I have on my view file for my form users group.
Code:
if (trim(!$user_group_id)) {
echo form_open('admin/users_group/add', array('class' => 'form-horizontal', 'role' => 'form', 'id' => "form-user-group"));
} else {
echo form_open('admin/users_group/edit/' . $user_group_id, array('class' => 'form-horizontal', 'role' => 'form','id' => "form-user-group"));
}
;?>

<div class="form-group required">
<label class="col-sm-2 control-label" for="input-name">&lt;?php echo $entry_name; ?&gt;</label>
<div class="col-sm-10">
&lt;input type="text" name="name" value="&lt;?php echo $name; ?&gt;" placeholder="&lt;?php echo $entry_name; ?&gt;" id="input-name" class="form-control" /&gt;
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label">&lt;?php echo $entry_access; ?&gt;</label>
<div class="col-sm-10">
<div class="well well-sm"  150px; overflow: auto;">
&lt;?php foreach ($permissions as $permission) { ?&gt;
<div class="checkbox">
<label>
&lt;?php if (in_array($permission, $access)) { ?&gt;
&lt;input type="checkbox" name="permission[access][]" value="&lt;?php echo $permission; ?&gt;" checked="checked" /&gt;
&lt;?php echo $permission; ?&gt;
&lt;?php } else { ?&gt;
&lt;input type="checkbox" name="permission[access][]" value="&lt;?php echo $permission; ?&gt;" /&gt;
&lt;?php echo $permission; ?&gt;
&lt;?php } ?&gt;
</label>
</div>
&lt;?php } ?&gt;
</div>
<a  true);">&lt;?php echo $text_select_all; ?&gt;</a> / <a  false);">&lt;?php echo $text_unselect_all; ?&gt;</a>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label">&lt;?php echo $entry_modify; ?&gt;</label>
<div class="col-sm-10">
<div class="well well-sm"  150px; overflow: auto;">
&lt;?php foreach ($permissions as $permission) { ?&gt;
<div class="checkbox">
<label>
&lt;?php if (in_array($permission, $modify)) { ?&gt;
&lt;input type="checkbox" name="permission[modify][]" value="&lt;?php echo $permission; ?&gt;" checked="checked" /&gt;
&lt;?php echo $permission; ?&gt;
&lt;?php } else { ?&gt;
&lt;input type="checkbox" name="permission[modify][]" value="&lt;?php echo $permission; ?&gt;" /&gt;
&lt;?php echo $permission; ?&gt;
&lt;?php } ?&gt;
</label>
</div>
&lt;?php } ?&gt;
</div>
<a  true);">&lt;?php echo $text_select_all; ?&gt;</a> / <a  false);">&lt;?php echo $text_unselect_all; ?&gt;</a></div>
</div>

&lt;/form&gt;

And on the getForm()

Code:
protected function getForm() {
  $this->load->model('admin/user/users_group_model');
  $this->load->library('request');

  $data['heading_title'] = $this->lang->line('heading_title');

  $data['text_select_all'] = $this->lang->line('text_select_all');
  $data['text_unselect_all'] = $this->lang->line('text_unselect_all');

  $data['entry_name'] = $this->lang->line('entry_name');
  $data['entry_access'] = $this->lang->line('entry_access');
  $data['entry_modify'] = $this->lang->line('entry_modify');

  $data['button_save'] = $this->lang->line('button_save');
  $data['button_cancel'] = $this->lang->line('button_cancel');

  $data['breadcrumbs'] = array();

  $data['breadcrumbs'][] = array(
   'text' => $this->lang->line('text_home'),
   'href' => site_url('admin/dashboard')
  );

  $data['breadcrumbs'][] = array(
   'text' => $this->lang->line('heading_title'),
   'href' => site_url('admin/users_group')
  );

  $data['cancel'] = site_url('admin/users_group');

  if (!empty($this->error['warning'])) {
   $data['error_warning'] = $this->error['warning'];
  } else {
   $data['error_warning'] = '';
  }

  if (!empty($this->session->flashdata('success'))) {
   $data['success'] = $this->session->flashdata('success', $this->lang->line('text_success'));
  } else {
   $data['success'] = '';
  }

  $user_group_id = $this->uri->segment(4);
  $data['user_group_id'] = $user_group_id;

  if (isset($user_group_id)) {
   $data['action'] = site_url('user/users_group/add');
  } else {
   $data['action'] = site_url('admin/users_group/edit/' . $user_group_id);
  }

  if (!empty($user_group_id) && $this->request->server['REQUEST_METHOD'] != 'POST') {
   $user_group_info = $this->users_group_model->getUserGroup($user_group_id);
  }

  if (isset($this->request->post['name'])) {
   $data['name'] = $this->request->post['name'];
  } elseif (!empty($user_group_info)) {
   $data['name'] = $user_group_info['name'];
  } else {
   $data['name'] = '';
  }

  $ignore = array(
   'blank',
   'error',
   'register',
   'dashboard',
   'column_left',
   'menu',
   'startup',
   'login',
   'logout',
   'forgotten',
   'reset',
   'not_found',
   'permission',
   'footer',
   'header'
  );

  $data['permissions'] = array();

  $files = glob(APPPATH . 'modules/admin/' . 'controllers/*/*.php');

  foreach ($files as $file) {
   $part = explode('/', dirname($file));

   $permission = basename($file, '.php');

   if (!in_array($permission, $ignore)) {
    $data['permissions'][] = $permission;
   }
  }
  
  if (isset($this->request->post['permission']['access'])) {
   $data['access'] = $this->request->post['permission']['access'];
  } elseif (isset($user_group_info['permission']['access'])) {
   $data['access'] = $user_group_info['permission']['access'];
  } else {
   $data['access'] = array();
  }

  if (isset($this->request->post['permission']['modify'])) {
   $data['modify'] = $this->request->post['permission']['modify'];
  } elseif (isset($user_group_info['permission']['modify'])) {
   $data['modify'] = $user_group_info['permission']['modify'];
  } else {
   $data['modify'] = array();
  }

  $this->load->view('user/users_group_form', $data);

}

#4
[eluser]riwakawd[/eluser]
Problem Solved Now Had To Do this

Code:
public function editUserGroup($user_group_id, $data)
{
    $permission = (isset($data['permission']) ? serialize($data['permission']) : null);
    $update = array(
        'name' => $data['name'],
        'permission' => $permission,
    );
    $this->db->where('user_group_id', $user_group_id);
    $this->db->update($this->db->dbprefix . 'user_group', $update);
}

#5
[eluser]riwakawd[/eluser]
[quote author="CroNiX" date="1413565435"]Totally standard HTML form behavior. Unchecked boxes do not get transmitted with form data, so you need to check to see if it was posted and if it wasn't, then no boxes were checked and take appropriate action.[/quote]

Just to let you know problem is solved posted fix.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.