2014-03-19 72 views
0

我正在codeigniter php中工作。我想爲每個選中的複選框值插入多行,並使用oyhers值。 t試圖做到這一點。但它顯示我數據庫陣列錯誤。使用codeigniter將多個複選框中的每個「checked」複選框值插入數據庫

鑑於:

類別名稱:

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

} 

這個代碼顯示我數據庫陣列錯誤。所以現在我怎麼解決這個問題?

+0

您可以發佈消息數組?在'save_content'裏面也顯示'print_r($ data)'? –

回答

0

Move $this->co_model->save_content($data);在foreach循環中。

試試這個代碼

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