繁体   English   中英

如何使用Codeigniter将数组插入MySQL?

[英]How to insert arrays into MySQL using Codeigniter?

关于我的项目,我希望每个employee{employee_no}插入employee{employee_no}许多描述voucher_noamount ,以及许多支持文档 ,所以我创建了我的视图页面以获取描述和支持文档添加多个字段。

所以我可以根据需要添加许多字段,但是当我插入没有描述和支持文档的数据时,我想将这些数据正常插入到我的数据库中。

它工作得很好,但我的要求是使这段代码插入多个描述和支持文档。

这是我的View page :字段到描述和支持文档

<div class="form-group">
        <div class="row colbox">
        <div class="col-sm-offset-2 col-lg-8 col-sm-8 text-left">Description</div>

        <div class="field_wrapper">
            <input type="textarea" name="descrip[]" value="" size="35px" /><input type="text" name="voucher_no[]" value="" size="7px"/><input type="text" name="price[]" value=""size="7px"/>
            <a href="javascript:void(0);" class="add_button" title="Add field"><img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>
        </div></div></div>

        <div class="form-group">
        <div class="row colbox">
        <div class="col-sm-offset-2 col-lg-8 col-sm-8 text-left">SUPPORT DOCUMENT</div><br />

        <div class="field_upload">
            <input type="file" name="field_upload[]" value="" size="35px" />
            <a href="javascript:void(0);" class="add_butt" title="Add field">
            <img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>
        </div></div></div>

我的控制器:

  function index()
    {
        //fetch data from department and designation tables
        $data['department'] = $this->employee_model->get_department();
        $data['designation'] = $this->employee_model->get_designation();
        //$data['descrip'] = $this->employee_model->insert_descriptions();

        //set validation rules
        $this->form_validation->set_rules('employeeno', 'Employee No', 'trim|required|numeric');
        $this->form_validation->set_rules('employeename', 'Employee Name', 'trim|required|callback_alpha_only_space');
        $this->form_validation->set_rules('department', 'Department', 'callback_combo_check');
        $this->form_validation->set_rules('designation', 'Designation', 'callback_combo_check');
        $this->form_validation->set_rules('hireddate', 'Hired Date');
        $this->form_validation->set_rules('salary', 'Salary', 'required|numeric');

        $this->form_validation->set_rules('document', 'Document', 'trim');
        $this->form_validation->set_rules('descrip', 'Description', 'trim');

        if ($this->form_validation->run() == FALSE)
        {
            //fail validation
            $this->load->view('employee_view', $data);
        }
        else
        {    
            //pass validation
            $data = array(
                'employee_no' => $this->input->post('employeeno'),
                'employee_name' => $this->input->post('employeename'),
                'department_id' => $this->input->post('department'),
                'designation_id' => $this->input->post('designation'),
                'hired_date' => @date('Y-m-d', @strtotime($this->input->post('hireddate'))),
                'salary' => $this->input->post('salary'),
                'description' => $this->input->post('descrip'),
'document' => $this->input->post('filed_upload'),

            );

            //insert the form data into database
            $this->db->insert('tbl_employee', $data);

我知道我想使用foreach循环来获取描述支持文档,但我不知道如何使用它

您可以使用insert_batch For Eg:

$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. 这将产生一个冗余数据,但它是直接的解决方案,我不会喜欢。

在描述和文档数组的基础上循环插入数组。(这些都将作为数组发布)然后用于批处理插入

$this->db->insert_batch();
  1. 您可以使用implode并以逗号分隔值转换这些数组,并将它们插入employee表。 我也不会喜欢这个。

  2. 第三是最好的。 制作另外两个表描述和文档(要存储多个描述相关数据和用于插入多个文档的文档),这两个表将包含employee表的外键。 一个员工条目和其他描述和文档条目。 此链接可以帮助您进行查询。

http://www.codeigniter.com/userguide2/database/active_record.html#update

您可以获取记录的计数,然后使用for循环创建要插入数据库的数据数组。

$records = count($_POST['employee_no'];

for($i=0; $i<$records; $i++) {
    $data = array(
        'employee_no' => $_POST['employeeno'][$i],
        'employee_name' => $_POST['employeename'][$i]
        ..etc
    );

    $this->db->insert('tbl_employee', $data);
}

暂无
暂无

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

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