使用codeigniter将多个复选框中的每个“已选中”复选框值插入数据库

时间:2014-03-19 10:00:02

标签: php arrays codeigniter checkbox

我在codeigniter php工作。我想为每个选中的复选框值插入多行与oyhers值。我试着去做。但它显示了我的数据库数组错误。

在视图中:

类别名称:

 <?php

           foreach($result as $aresult)
               {

                ?>
      <input type="checkbox" name="category_name[]" value="<?php echo $aresult->category_name;?>" /> <?php echo $aresult->category_name;?> <br>
              <?php
                }
                  foreach($area as $aresult1)
                    {

                ?>   
                <input type="checkbox" name="category_name[]" value="<?php echo $aresult1->category_name;?>" /> <?php echo $aresult1->category_name;?> <br>
             <?php 

                    } 
                ?>
  <tr>
        <td>Content Headline:</td>
        <td>

                <input type="text" required="1" name="content_headline" tabindex="3" placeholder="Content Headline" size="80"/>

        </td> 
    </tr>  

<tr>
        <td>Picture</td>
        <td>
            <input type="file" name="image" tabindex="8"/>                
        </td>
    </tr>

   <tr>
        <td colspan="2" align="center"><input type="submit" name="btn"  value="Save" tabindex="9"/></td>
    </tr>

控制器:

     public function savecontent()
    {
        date_default_timezone_set('Asia/Dhaka');
       foreach($this->input->post('category_name') as $rm){           
        $data=array(

        $now = date("Y-m-d H:i:s"),

        $data['admin_id']=$this->session->userdata('admin_id'),
        $data['category_name']=$this->input->post('category_name',true),
        $data['content_headline']=$this->input->post('content_headline',true),
      $this->load->library('upload');
    $config['upload_path'] = './images/news_images/';
    $config['allowed_types'] = 'gif|jpg|png|mp3';
    $config['max_size'] = '100000';
    $config['max_width'] = '1024';
    $config['max_height'] = '720';
    $error = '';
    $udata = '';
    $udata1 = '';
    $udata2 = '';
    $udata3 = '';

    $this->upload->initialize($config);
    if (!$this->upload->do_upload('image')) {
        $error = array('error' => $this->upload->display_errors());

    }
    else {

        $udata = array('upload_data' => $this->upload->data()); 
         $data['image'] = "images/news_images/" . $udata['upload_data']['file_name'];

    }

    );

       }     

    $this->co_model->save_content($data);
  }

型号:

  public function save_content($data)
{

        $this->db->insert('content',$data);

}

此代码显示数据库数组错误。那我现在如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在foreach循环中移动$this->co_model->save_content($data);

试试此代码

public function savecontent()
{
    date_default_timezone_set('Asia/Dhaka');
    foreach($this->input->post('category_name') as $rm)
    {           
        $data=array();

        $now = date("Y-m-d H:i:s"),

        $data['admin_id']=$this->session->userdata('admin_id'),
        $data['category_name']=$this->input->post('category_name',true),
        $data['content_headline']=$this->input->post('content_headline',true),
        $this->load->library('upload');
        $config['upload_path'] = './images/news_images/';
        $config['allowed_types'] = 'gif|jpg|png|mp3';
        $config['max_size'] = '100000';
        $config['max_width'] = '1024';
        $config['max_height'] = '720';
        $error = '';
        $udata = '';
        $udata1 = '';
        $udata2 = '';
        $udata3 = '';

        $this->upload->initialize($config);
        if (!$this->upload->do_upload('image'))
        {
            $error = array('error' => $this->upload->display_errors());        
        }
        else 
        {        
            $udata = array('upload_data' => $this->upload->data()); 
            $data['image'] = "images/news_images/" . $udata['upload_data']['file_name'];        
        }
        $this->co_model->save_content($data);
     } 
}