输入多个数据Codeigniter

时间:2017-11-23 18:15:03

标签: codeigniter

很抱歉,如果之前已经提出这个问题。我想请求帮助以查看下面的脚本。因为当我尝试使用多个输入字段提交表单时,它只会生成一个数据,而应该有两个数据输入到数据库中。

那么,我的剧本的哪一部分是错的?

控制器

public function add() {
    // ... some script before 'else' ...
    } else {

        $post = $this->input->post();
        $result = array();
        $total_input = count($post['input_acc_code']);
        foreach ($post['input_acc_code'] as $key => $value) {
            $result[] = array(
                'trans_type' => 'journal',
                'form_type' => NULL,
                'acc_code' => $post['input_acc_code'][$key],
                'acc_type_id' => $post['input_acc_type_id'][$key],
                'refference' => '',
                'customer_ID' => NULL,
                'acc_side' => '',
                'debet' => $post['input_debet'][$key],
                'credit' => $post['input_credit'][$key],
                'summary' => $post['input_note'][$key],
                'files' => NULL,
                'create_at' => date("Y-m-d H:i:s", strtotime("now"))
                );
            if($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) {

               $this->session->set_flashdata('alert', 'Success');
               redirect(base_url().'admin/transaction');
            } else {
               $this->session->set_flashdata('alert', 'Failed');
               redirect(base_url().'admin/transaction');
            }
        }
    }
}

模型

function savedata($table, $data = array()) {
    $this->db->insert_batch($table, $data);
    if($this->db->affected_rows() > 0) {
        return TRUE;
    }
    return FALSE;
}

查看

<?php $attributes = array('class' => 'form-horizontal', 'id' => '');
echo form_open_multipart(base_url().$this->session->userdata('user_status').'/transaction/add', $attributes);?>
<div class="row">
    <div class="col-sm-12 col-md-12 panel-form-input">
        <div class="form-group form-group-sm">
            <label for="input_datetime" class="col-sm-2 control-label">Tanggal Transaksi</label>
            <div class="col-sm-10">
                <input type="text" class="input-date form-control" name="input_datetime[]" id="input-date">
                <?php echo form_error('input_datetime');?>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-sm-12 col-md-12 panel-form-input">
        <div class="panel panel-default">
            <div class="table-responsive">
                <table class="table table-unbordered">
                    <thead>
                        <th class="col-25">Account</th>
                        <th class="col-5">Account Type</th>
                        <th class="col-35">Notes</th>
                        <th class="col-15">Debet</th>
                        <th class="col-15">Credit</th>
                        <th class="col-5"></th>
                    </thead>
                    <tbody>

                        // First Input Field Form Table

                        <tr>
                            <td class="col-25">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1">
                                        <?php if ($account_list != NULL): ?>
                                            <option>— Choose Account Number —</option>
                                            <?php foreach ($account_list as $value): ?>
                                            <option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option>
                                            <?php endforeach;?>
                                            <?php else:?>
                                            <option>— No Data —</option>
                                        <?php endif;?>
                                        </select>
                                        <?php echo form_error('input_acc_code[]');?>
                                    </div>
                                </div>
                            </td>
                            <td class="col-5">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1">
                                        <?php echo form_error('input_acc_type_id[]');?>
                                    </div>
                                </div>
                            </td>
                            <td class="col-35">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_note[]">
                                        <?php echo form_error('input_note[]');?>
                                    </div>
                                </div>
                            </td>
                            <td class="col-15">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_debet[]">
                                    </div>
                                </div>
                            </td>
                            <td class="col-15">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_credit[]">
                                    </div>
                                </div>
                            </td>
                            <td class="col-5"></td>
                        </tr>

                        // Second Input Field Form Table

                        <tr>
                            <td class="col-25">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1">
                                        <?php if ($account_list != NULL): ?>
                                            <option>— Choose Account Number —</option>
                                            <?php foreach ($account_list as $value): ?>
                                            <option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option>
                                            <?php endforeach;?>
                                            <?php else:?>
                                            <option>— No Data —</option>
                                        <?php endif;?>
                                        </select>
                                        <?php echo form_error('input_acc_code[]');?>
                                    </div>
                                </div>
                            </td>
                            <td class="col-5">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1">
                                        <?php echo form_error('input_acc_type_id[]');?>
                                    </div>
                                </div>
                            </td>
                            <td class="col-35">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_note[]">
                                        <?php echo form_error('input_note[]');?>
                                    </div>
                                </div>
                            </td>
                            <td class="col-15">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_debet[]">
                                    </div>
                                </div>
                            </td>
                            <td class="col-15">
                                <div class="form-group form-group-sm">
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="input_credit[]">
                                    </div>
                                </div>
                            </td>
                            <td class="col-5"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-sm-12 col-md-12">
        <div class="menu-bar">
            <button class="btn btn-md btn-primary" type="submit">Save</input>
        </div>
    </div>
</div>
<?php echo form_close();?>

感谢您的帮助......

1 个答案:

答案 0 :(得分:0)

尝试将insert_batch相关模型调用移出foreach,否则您可以在那里直接插入。首先,您要构建要插入的查询,然后在foreach之后插入多维数组!

public function add()
{
    $post = $this->input->post();
    $result = array();
    $total_input = count($post['input_acc_code']);
    foreach ($post['input_acc_code'] as $key => $value) {
        $result[] = array(
            'trans_type'  => 'journal',
            'form_type'   => NULL,
            'acc_code'    => $post['input_acc_code'][$key],
            'acc_type_id' => $post['input_acc_type_id'][$key],
            'refference'  => '',
            'customer_ID' => NULL,
            'acc_side'    => '',
            'debet'       => $post['input_debet'][$key],
            'credit'      => $post['input_credit'][$key],
            'summary'     => $post['input_note'][$key],
            'files'       => NULL,
            'create_at'   => date("Y-m-d H:i:s", strtotime("now"))
        );
    }        
    if ($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) {
        $this->session->set_flashdata('alert', 'Success');
        redirect(base_url() . 'admin/transaction');
    } else {
        $this->session->set_flashdata('alert', 'Failed');
        redirect(base_url() . 'admin/transaction');
    }
}