2

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);
        }
    }
}
3
  • show us your code. what you have tried so far ? Commented Sep 17, 2016 at 8:50
  • Hey where is ur insert_batch function ?? Commented Sep 17, 2016 at 9:59
  • Proof read your question before submit Commented Sep 17, 2016 at 10:14

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You need to tell us if it didn't worked & what error you are getting
it gives blank view page and database column is empty
We are not returning to view page here so you need to handle it by passing insert id if you want but debug your issue tell me what it gives you in $data,if you print_r($data); just above model call in controller
Also can you tell what is $pre_id in your model code
1

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

7 Comments

you can't save the array in database directly, you need to data encode to json format.
well i am using codeigniter as my framework for about a year, and this works fine for me! i am not using that encode to json format thing, i am just using what is in the manual
Yes, I tried that also, it generates 500 error. @ GGsThePlayDude
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); } }
when i use your code print_r will give this output:- Array ( [medicine_name] => Array ( [0] => sss [1] => vvv ) ) But records not inserted in table @ GGsThePlayDude
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.