简体   繁体   English

在Codeigniter中插入多行MySQL

[英]Inserting multiple rows mysql in codeigniter

i am trying to insert multiple rows at once using Codeigniter 我试图使用Codeigniter一次插入多行

my html code for codeigniter view 我的用于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>

and i am using jQuery to insert new row in the table here is js the code 我正在使用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');
});

my controller 我的控制器

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

model 模型

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

i tried too many solutions from stackoverflow but none of them worked 我从stackoverflow尝试了太多解决方案,但没有一个起作用

Can you please try below given code? 您可以尝试使用下面给出的代码吗? I think there may be issue with $data. 我认为$ 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);
    }

if your php version>=5.4 it should work. 如果您的PHP版本> = 5.4,它应该可以工作。
You may try this 你可以试试这个

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM