0

我有两个表 ::: tbl_product 和 tbl_featured_product。我确实通过复选框系统查看了视图页面中的所有产品(来自 tbl_product)信息。这样我就可以根据需要通过检查将数据提交给 tbl_featured_product 。

我可以在复选框中查看所有信息到查看页面。但无法将它们保存在数据库中。只保存最后一行。请帮我同时保存多个数据:::

看法:::

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

我的控制器:::::

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

我的模特 ::::

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

请让我知道您方面的解决方案。谢谢

4

3 回答 3

0

您的问题是输入$this->input->post('product_name')是数组,因此您需要为每个插入一行,例如:

(在模型中)

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; 
}
于 2013-09-17T08:53:40.047 回答
0

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

$this->db->insert_batch('your_table', $temp);
于 2013-09-17T09:03:13.363 回答
0

做这样的事情,你可能需要做一些相应的改变:

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);
        }
    }
}
于 2013-09-17T08:59:19.637 回答