模型不插入数组数据

时间:2015-03-18 05:46:41

标签: codeigniter

当我提交表单时,我的controller []数组帖子不起作用会引发错误。

错误1

  

遇到PHP错误严重性:通知消息:数组到字符串   转换文件名:mysqli / mysqli_driver.php行号:544

     

错误号:1054'字段列表'中的未知列'数组'INSERT INTO   user_groupnamecontrolleraccessmodify)价值观   ('Admin',Array,'1','1')文件名:   C:\ XAMPP \ htdocs中\ riwakawebsitedesigns \ SYSTEM \数据库\ DB_driver.php   行号:331

未插入控制器名称。不确定最好的修复方法吗?

模型

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

控制器

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

        }
    }
}

?>

查看

<?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 个答案:

答案 0 :(得分:1)

您的controller隐藏字段是一个数组。您将此数组传递给addUserGroup函数,该函数尝试将此数组插入数据库。它隐含地尝试将此数组转换为字符串。也许尝试将您的功能更改为:

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

答案 1 :(得分:1)

错误是因为您无法在数据库中插入php-array。 而是存储逗号分隔值。 在您的模型中更改data数组,如下所示:

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

答案 2 :(得分:0)

问题修复模型中的foreach感谢关于如何解决每个问题的想法。

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