简体   繁体   中英

Insert each “checked” check box values from multiple checkboxes to the database using codeigniter

i am working in codeigniter php. i want to insert multiple rows for each checked checkbox value with oyhers value. t tried to do it. but it shows me database array error.

In view :

Category Name:

 <?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>

Controller:

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

Model:

  public function save_content($data)
{

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

}

this code shows me database array error. so now how i solve this problem?

Move $this->co_model->save_content($data); inside foreach loop.

Try this code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM