在codeigniter中插入多行mysql

时间:2015-03-28 11:30:03

标签: php jquery codeigniter

我正在尝试使用Codeigniter

一次插入多行

我的代码签名视图的HTML代码

     <?= form_open(base_url() . 'home/create') ?>
            <tbody>

                <tr>
                    <td class="col-md-1 center-block">
                        <input type="text" name="id[]" value="1" class="form-control" />
                    </td>
                    <td>
                        <input type="text" placeholder="enter descreption" class="form-control" name="desc[]" />
                    </td>
                    <td class="col-md-2 center-block">
                        <input id="space"  onkeyup="sumPrice()" type="number" class="form-control" name="space[]" />
                    </td>
                    <td> 
                        <select onchange="sumPrice()" class="selectpicker price" data-live-search="true" data-width="100%" name="price[]" data-header="Select cost"> 
                            <option value="5">5</option> 
                            <option value="10">10</option>
                            <option value="20">20</option>
                        </select>
                    </td> 
                    <td class="total_price col-md-1">
                        <input type="number" class="form-control" name="total[]" value="1000" />
                    </td> 
                    <td>
                        <button class="btn btn-danger center-block  remove-row"><span class="glyphicon glyphicon-remove"></span> Remove</button>
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" value="insert" class="btn btn-success" />
    </form>
    <button class="btn btn-success append-row"><span class="glyphicon glyphicon-plus-sign"></span> Add new row</button>

我正在使用jQuery在表中插入新行 这是js代码

    $('.append-row').click(function() {
    $("table tbody").append
    ('<tr><td class="col-md-1 center-block"><input type="text" name="id[]" value="' + row_id_val++ + '" class="form-control" /></td> <td><input type="text" placeholder="enter descreption" class="form-control" name="desc[]" /></td> <td class="col-md-2 center-block"><input id="space"  onkeyup="sumPrice()" type="number" class="form-control" name="space[]" /></td> <td> <select onchange="sumPrice()" class="selectpicker price" data-live-search="true" data-width="100%" name="price[]" data-header="Select Sub account" onselect="sumPrice()" > <option onselect="sumPrice()" value="5">5</option> <option onselect="sumPrice()" value="10">10</option> <option onselect="sumPrice()" value="20">20</option> </select> </td> <td class="total_price col-md-1"><input type="number" class="form-control" value=""  name="total[]" /></td> <td><button class="btn btn-danger center-block  remove-row"><span class="glyphicon glyphicon-remove"></span> Remove</button></td></tr>');
    $('.selectpicker').selectpicker('refresh');
});

我的控制器

if ($_POST) {
    $data = array();
    for ($i = 0; $i < count($this->input->post('id')); $i++) {
        $data[$i] = array(
            'row_id' => $this->input->post('id')[$i],
            'desc' => $this->input->post('desc')[$i],
            'space' => $this->input->post('space')[$i],
            'price' => $this->input->post('price')[$i],
            'total' => $this->input->post('total')[$i],
            'code' => time()
        );
    }
    $this->Home_model->create($data);
}

模型

function create($data) {
    $this->db->insert_batch('quotation', $data);
}

我尝试了stackoverflow的太多解决方案,但没有一个工作

3 个答案:

答案 0 :(得分:3)

你可以试试下面给出的代码吗?我认为$ data可能存在问题。

if ($_POST) {
        $data = array();
        for ($i = 0; $i < count($this->input->post('id')); $i++) {
            $data[] = array(
                'row_id' => $this->input->post('id')[$i],
                'desc' => $this->input->post('desc')[$i],
                'space' => $this->input->post('space')[$i],
                'price' => $this->input->post('price')[$i],
                'total' => $this->input->post('total')[$i],
                'code' => time()
            );
        }
        $this->Home_model->create($data);
    }

答案 1 :(得分:1)

如果你的php版本&gt; = 5.4它应该可以工作。 你可以试试这个

    if ($_POST) 
    {
        $row_ids=$this->input->post('id');
        $desc=$this->input->post('desc');
        $spaces=$this->input->post('space');
        $prices=$this->input->post('price');
        $totals=$this->input->post('total');
        $data = array();
        for ($i = 0; $i < count($this->input->post('id')); $i++)
        {
            $data[$i] = array(
                'row_id' => $row_ids[$i],
                'desc' => $desc[$i],
                'space' => $spaces[$i],
                'price' => $prices[$i],
                'total' => $totals[$i],
                'code' => time()
            );
        }
        $this->Home_model->create($data);
    }

答案 2 :(得分:0)

尝试在标签表单中添加enctype =“ multipart / form-data”