簡體   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