简体   繁体   中英

Saving multiple checkbox data by foreach in MySQL using codeigniter

I have two tables ::: tbl_product and tbl_featured_product. I did view all product (from tbl_product) information in view pages by checkbox system. So that I can submit data to tbl_featured_product by checked as I need.

I can view all information to a view page in checkbox.. BUT can't save them in database. saving only last one row. please help me out to save multiple data in same time:::

view:::

<?php foreach($all_product as $values) { ?>

  <input type="checkbox" name="product_name[]" value="<?php echo $values->product_name;?>" /> <?php echo $values->product_name;?> <br>
  <input hidden="hidden" name="product_id[]" value="<?php echo $values->product_id;?>" /> 
  <input hidden="hidden" name="product_price[]" value="<?php echo $values->product_price;?>" /> 

<?php } ?>

<input type="submit" name="btn" value="Save">

My Controller:::::

 public function save_featured_product()
 {
    $data=array();

    if ($this->input->post()) {
        $data['featured_id']=$this->input->post('featured_id',true);
        $data['product_id']=$this->input->post('product_id',true);
        $data['product_name']=$this->input->post('product_name',true);
        $data['product_price']=$this->input->post('product_price',true);

        $this->sa_model->save_featured_product_info($data);

        $sdata=array();
        $sdata['message']='Save product Information Successfully !';
        $this->session->set_userdata($sdata);
        redirect('super_admin/add_featured_product');
    } 

My Model ::::

 public function save_featured_product_info($data)
  {
    $this->db->insert('tbl_featured_products',$data);
  }

Please let me know the solutions from your side. Thank you

Your problem is that the input $this->input->post('product_name') are arrays, so you need to insert one row for each, like:

(in the model)

public function save_featured_product_info($data)
{
if (isset($data['product_name']) && is_array($data['product_name'])):
    foreach ( $data['product_name'] as $key=>$value ):
        $this->db->insert('tbl_featured_products', array(
           'product_id'=>$data['product_id'][$key],
           'product_name'=>$data['product_name'][$key],
           'product_price'=>$data['product_price'][$key],
           'featured_id'=>$data['featured_id'] // assuming this are the same for all rows?
        ));
    endforeach;
endif; 
}

Do something like this, you may need to do some changes accordingly:

function save_featured_product_info($data){
    if( isset( $data['product_id'] ) && is_array( $data['product_id'] ) ){
        foreach( $data['product_id'] as $key => $each ){
            $temp[] = array(
                        'featured_id'  =>$data['featured_id'][$key],
                        'product_id'   =>$data['product_id'][$key],
                        'product_name' =>$data['product_name'][$key],
                        'product_price'=>$data['product_price'][$key],
                      );
        }
        if( isset( $temp ) ){
            $this->db->insert_batch('tbl_featured_products', $temp);
        }
    }
}

您正在尝试保存数组,您需要内插值或循环并单独保存它们或批量插入它们

$this->db->insert_batch('your_table', $temp);

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