将多个数据插入到使用PHP Codeigniter填充的数据库中

时间:2015-01-18 08:22:25

标签: php codeigniter

我有一个表单,我想将所有多个数据保存到我的数据库,但<div> class="items"</div>填充,所以这里是我的PHP代码

<form action="<?php echo base_url().'index.php/water/save_items' ?>" method="post">
<input id="uid" name="uid" value="5" type="hidden">
<div class="items">
    <input name="pid" value="1" type="hidden"><input name="item" value="6" type="hidden">
    <input name="pid" value="12" type="hidden"><input name="item" value="9" type="hidden">
    <input name="pid" value="11" type="hidden"><input name="item" value="56" type="hidden">
    <input name="pid" value="10" type="hidden"><input name="item" value="14" type="hidden">
    <input name="pid" value="14" type="hidden"><input name="item" value="4" type="hidden">
    <input name="pid" value="1" type="hidden"><input name="item" value="17" type="hidden">
</div>
<button id="checkout" type="button" class="btn btn-success product-button">Checkout</button>

CONTROLLER

public function save_items()
{
   $uid = $this->input->post('uid');
   $array = $this->input->post();
   $this->load->model('product_model');
   if(!empty($array))
   {
       foreach($array as $pid => $item) 
       {
          $this->product_model->addProduct($uid,$pid,$item);
       }
   }

   redirect('water/products');
}

product_model.php

public function addProduct($uid, $pid, $item)
{
   $data = array('uid'=>$uid, 'pid'=>$pid, 'item'=>$item);
   $this->db->insert('cart',$data);
}

我有想法保存uid但我不知道如何从表单中获取并保存“pid”和“item”的值

预期的数据库值应该是这样的

uid     pid     item
 5      1       6
 5      12      9
 5      11      56
 5      10      14
 5      14      4
 5      1       17

2 个答案:

答案 0 :(得分:0)

        $data = array(
        array(
        'title' => 'My title' ,
        'name' => 'My Name' ,
        'date' => 'My date'
        ),
        array(
        'title' => 'Another title' ,
        'name' => 'Another Name' ,
        'date' => 'Another date'
        )
        );

        $this->db->insert_batch('mytable', $data); 

        // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')

答案 1 :(得分:0)

使用pid []和item []数组而不是pid,item

<div class="items">
        <input name="pid[]" value="1" type="hidden"><input name="item[]" value="6" type="hidden">
        <input name="pid[]" value="12" type="hidden"><input name="item[]" value="9" type="hidden">
        <input name="pid[]" value="11" type="hidden"><input name="item[]" value="56" type="hidden">
        <input name="pid[]" value="10" type="hidden"><input name="item[]" value="14" type="hidden">
        <input name="pid[]" value="14" type="hidden"><input name="item[]" value="4" type="hidden">
        <input name="pid[]" value="1" type="hidden"><input name="item[]" value="17" type="hidden">
    </div>

//并在此功能中执行以下操作

public function save_items()
{
   $uid = $this->input->post('uid');
   $item_data = $this->input->post('item');
   $pid_data = $this->input->post('pid');
   $this->load->model('product_model');

 for($i = 0; $i < count($item_data); $i++)
    {
        $pid = $pid_data[$i];
        $item = $item_data[$i];
        $this->product_model->addProduct($uid,$pid,$item);
    }


   redirect('water/products');
}