簡體   English   中英

如何使用codeigniter在mysql的單列中插入數組數據

[英]how to insert array data in single column in mysql using codeigniter

我正在嘗試使用codeigniter將數組數據插入數據庫。 當我使用print_r()來打印該值時很有趣。

它顯示正確的結果,但是當我單擊“保存”按鈕時,它將在表中插入空白值。

我只想在特定處方表中的處方表中存儲多個葯物名稱,我正在嘗試使用insert_batch函數插入數據,但是會保存空白記錄

這是我的模型代碼:

public function add_prescription($data) {

    $data['medicine_name'] = $data['medicine_nm[]'];
    print_r($data['medicine_name']);

    $this->db->insert_batch('pre',$data);
     return $this->db->insert_id($pre_id);
  }

控制器代碼-

public function prescription($patient_id = NULL, $app_date = NULL, $hour = NULL , $min = NULL) {

    //Check if user has logged in 
    if (!$this->session->userdata('user_name') || $this->session->userdata('user_name') == '') {
        redirect('login/index/');

    } else {

        if ($this->form_validation->run() === FALSE) {
          $data['medicine_nm[]']=$this->input->post('medicine_nm[]');   
           $this->patient_model->add_prescription($data);
        }
    }
}

您需要json_encode該數組,然后將其插入數據庫,例如

$data = json_encode($array);

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

在獲取時,您需要對其進行decode

$data = json_decode($column_result);

希望這對您有所幫助。

嘗試這個

public function add_prescription($name) {


$data = array(
 'medicine_name' => $name
  );

  $query = $this->db->insert('table_name',$data);

  return $query->result_array();
 }

希望這個能對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM