1

When I submit my form my controller[] array post does not work throws errors.

Error 1

A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: mysqli/mysqli_driver.php Line Number: 544

Error Number: 1054 Unknown column 'Array' in 'field list' INSERT INTO user_group (name, controller, access, modify) VALUES ('Admin', Array, '1', '1') Filename: C:\Xampp\htdocs\riwakawebsitedesigns\system\database\DB_driver.php Line Number: 331

It is not inserting the controller names. Not sure best way to fix?

Model

<?php

class Model_user_group extends CI_Model {

    public function addUserGroup($data) {
        $data = array(
            'name' => $this->input->post('name'),
            'controller' => $this->input->post('controller'),
            'access' => $this->input->post('access'),
            'modify' => $this->input->post('modify')
        );

        $this->db->set($data);
        $this->db->insert_id();
        $this->db->insert($this->db->dbprefix . 'user_group');
    }
?>

Controller

<?php

class Users_group extends Admin_Controller {

    public function index() {

        $data['title'] = "Users Group";

        $this->load->model('admin/user/model_user_group');

        $user_group_info = $this->model_user_group->getUserGroup($this->uri->segment(4));

        if ($this->input->post('name') !== FALSE) {
            $data['name'] = $this->input->post('name');
        } else {
            $data['name'] = $user_group_info['name'];
        }

        $ignore = array(
            'admin',
            'login',
            'dashboard',
            'filemanager',
            'login',
            'menu',
            'register',
            'online',
            'customer_total',
            'user_total',
            'chart',
            'activity',
            'logout',
            'footer',
            'header',
            'permission'
        );

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

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

        foreach ($files as $file) {
            $controller =  basename(strtolower($file), '.php');

            if (!in_array($controller, $ignore)) {
                $data['controllers'][] = $controller;
            }
        }

        if ($this->input->post('name') !== FALSE) {
            $data['controller'] = $this->input->post('controller');
        } else {
            $data['controller'] = $user_group_info['controller'];
        }

        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'User Group Name', 'required');

        if ($this->form_validation->run($this) == FALSE) {

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

        } else {

            $this->load->model('admin/user/model_user_group');

            $this->model_user_group->addUserGroup($this->input->post());

            redirect('admin/users_group');

        }
    }
}

?>

View

<?php echo validation_errors('<div class="alert alert-warning text-center"><i class="fa fa-exclamation-triangle"></i>
 ', '</div>'); ?>

<?php if ($this->uri->segment(4) == FALSE) { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open('admin/users_group/add', $data);?>
<?php } else { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open('admin/users_group/edit' .'/'. $this->uri->segment(4), $data);?>
<?php } ?>

<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('User Group Name', 'name', $data);?>
<div class="col-sm-10">
<?php $data1 = array('id' => 'name', 'name' => 'name', 'class' => 'form-control', 'value' => $name);?>
<?php echo form_input($data1);?>
</div>
</div>

<table class="table table-striped table-bordered">
<thead>
    <tr>
        <td>Controller Name</td>
        <td>Access</td>
        <td>Modify</td>
    </tr>
</thead>
<?php foreach ($controllers as $controller) {?> 
<tbody>
<tr>
<td><?php echo $controller;?>
<input type="hidden" name="controller[]" value="<?php echo $controller;?>" />
</td>
<td>
<select name="access" class="form-control">
  <option>1</option>
  <option>0</option>
</select>   
</td>
<td>
<select name="modify" class="form-control">
  <option>1</option>
  <option>0</option>
</select>
</td>
</tr>
</tbody>
<?php } ?>
</table>

<?php echo form_close();?>

3 Answers 3

1

The error is because you cannot insert php-array in database. Instead store comma separated values. In your model change data array as below:

public function addUserGroup($data) {
    $controllers = $this->input->post('controller');
    $name = $this->input->post('name');   
    $access = $this->input->post('access');
    $modify = $this->input->post('modify');

    for($i=0;$i<count($controllers);$i++) {
     $data = array(
      'name' => $name,
      'controller' => $controllers[$i],
      'access' => $access,
      'modify' => $modify
    );

    $this->db->set($data);
    $this->db->insert_id();
    $this->db->insert($this->db->dbprefix . 'user_group');
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That inserts each name users,users_group,users_group1,t into the controller column each controller name should have its own row is what trying to achieve
Than single insert statement will not work. You will have loop through each controller and in the loop write insert statement
1

Your controller hidden field is an array. You're passing this array to your addUserGroup function, which is trying to insert this array into the database. It's implicitly trying to convert this array to a string. Maybe try changing your function to this:

'controller' => $this->input->post('controller')[0],

Comments

0

Problem fixed foreach in model Thanks for the ideas on how to fix problems every one.

foreach ($this->input->post('controller') as $controller) {
        $data = array(
            'name' => $this->input->post('name'),
            'controller' => $controller,
            'access' => $this->input->post('access'),
            'modify' => $this->input->post('modify')
        );

        $this->db->set($data);
        $this->db->insert_id();
        $this->db->insert($this->db->dbprefix . 'user_group');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.