简体   繁体   English

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

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

Regarding my project I want insert per employee{employee_no} many descriptions with voucher_no and amount , and many support document , so I have created my view page for descriptions and support document add multiple fields. 关于我的项目,我希望每个employee{employee_no}插入employee{employee_no}许多描述voucher_noamount ,以及许多支持文档 ,所以我创建了我的视图页面以获取描述和支持文档添加多个字段。

So I can add many fields as many as I want but I want to insert those data into my database normally when I insert data without descriptions and support documents. 所以我可以根据需要添加许多字段,但是当我插入没有描述和支持文档的数据时,我想将这些数据正常插入到我的数据库中。

It works perfectly but my requirement is to make this code to insert multiple descriptions and support documents. 它工作得很好,但我的要求是使这段代码插入多个描述和支持文档。

this is my View page : fields to descriptions and support documents 这是我的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>

my controller : 我的控制器:

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

I know I want to use foreach loop for descriptons and support documents but I don't know how to use it 我知道我想使用foreach循环来获取描述支持文档,但我不知道如何使用它

You can use insert_batch For Eg: 您可以使用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')

you have three solutions for your problem. 你有三个问题的解决方案。

  1. This will generate a redundant data but its the straight solution, I will not prefer that. 这将产生一个冗余数据,但它是直接的解决方案,我不会喜欢。

loop around your insert array on the basis of description and document array.(these both will be posted as array) and then use this for batch insert 在描述和文档数组的基础上循环插入数组。(这些都将作为数组发布)然后用于批处理插入

$this->db->insert_batch();
  1. You can use implode and convert these arrays in comma separated values and insert them employee table. 您可以使用implode并以逗号分隔值转换这些数组,并将它们插入employee表。 I will not prefer this too. 我也不会喜欢这个。

  2. The third the best. 第三是最好的。 Make two other tables description and documents (To store multiple description related data and document for inserting multiple documents) these both will contain foreign key for employee table. 制作另外两个表描述和文档(要存储多个描述相关数据和用于插入多个文档的文档),这两个表将包含employee表的外键。 One employee entry and other description and document entries. 一个员工条目和其他描述和文档条目。 This link may help you with queries. 此链接可以帮助您进行查询。

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

You could get a count of the records, then use a for loop to create the data array to insert into the database. 您可以获取记录的计数,然后使用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