2
votes

I'm trying to insert array data into database using codeigniter. when I print that values using print_r() fun.

It shows correct result, but when I click on save button it inserts blank values in table.

I just want to store multiple medicine names in prescription table for particular prescription_id i am trying to insert data using insert_batch function but it saves blank records

this is my model code:

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

controller code--

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);
        }
    }
}
2
show us your code. what you have tried so far ? - Bhavin
Hey where is ur insert_batch function ?? - Shibon
Proof read your question before submit - Mr. ED

2 Answers

5
votes

You need to json_encode that array and then insert it into the database like

$data = json_encode($array);

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

And while fetching you need to decode it

$data = json_decode($column_result);

Hope this helps you.

1
votes

try this

public function add_prescription($name) {


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

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

  return $query->result_array();
 }

Hope this will help you