Insert Multiple rows Codeignite

I am trying to insert multiple data in each row., I have a dynamic input which i can add any field I want.

here’s my controller:

$doctorname = $this->input->post('doctorname');
		$doctorfield = $this->input->post('doctorfield');
		$date1 = $this->input->post('date1');
		$date11 = $this->input->post('date11');
		$date = $date1 . '-' . $date11;
		$data = array(
				'doctorname' => $doctorname,
				'doctorfield' => $doctorfield,
				'schedule' => $date
			
			);
		$this->load->model('emp_model');
		$this->emp_model->insertMonday($data);

and my model:

function insertMonday($data){
			
			foreach($data as $row){
				$this->db->insert('monday', $row);
			}
			
		}

I am getting an error of

Error Number: 1054

Unknown column ‘0’ in ‘field list’

INSERT INTO monday (0) VALUES (‘Test’)

Filename: /home/a1315895/public_html/models/emp_model.php

Line Number: 160

My input field has an array like this:

name=“date11[0]”

Please help me. What am I doing wrong?

Hi

// get arrays of values for each input field
$doctornames = $this->input->post('doctorname');
$doctorfields = $this->input->post('doctorfield');
$dates1 = $this->input->post('date1');
$dates11 = $this->input->post('date11');

// array for rows to insert
$rows = array();

// iterate one of input arrays and fill next row 
// with corresponding values from all other arrays
foreach($doctornames as $index => $doctorname){

    $doctorfield = $doctorfields[$index];
    $date = $dates1[$index] . '-' . $dates11[$index];

    $rows[] = array(
        'doctorname' => $doctorname,
        'doctorfield' => $doctorfield,
        'schedule' => $date
    );

}

$this->load->model('emp_model');
$this->emp_model->insertMonday($rows);

Hi, thanks for helping me. but still the same error appears.

A Database Error Occurred

Error Number: 1054

Unknown column ‘0’ in ‘field list’

INSERT INTO monday (0) VALUES (Array)

Filename: /home/a1315895/public_html/models/emp_model.php

Line Number: 159

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

Line Number: 542

This is what inside my table monday:

id => auto increment;
doctorname => varchar
doctorfield => varchar
schedule => varchar

My Model:

function insertMonday($rows){

			$this->db->insert('monday', $rows);
			
		}

My controller:

function insertMonSched(){
		
		
		// get arrays of values for each input field
		$doctornames = $this->input->post('doctorname');
		$doctorfields = $this->input->post('doctorfield');
		$dates1 = $this->input->post('date1');
		$dates11 = $this->input->post('date11');

		// array for rows to insert
		$rows = array();

		// iterate one of input arrays and fill next row 
		// with corresponding values from all other arrays
		foreach($doctornames as $index => $doctorname){

			$doctorfield = $doctorfields[$index];
			$date = $dates1[$index] . '-' . $dates11[$index];

			$rows[] = array(
				'doctorname' => $doctorname,
				'doctorfield' => $doctorfield,
				'schedule' => $date
			);

		}
		$this->load->model('emp_model');
		$this->emp_model->insertMonday($rows);

Thanks :slight_smile:

That is because you removed loop from insertMonday() method
My code supposes that loop is still there

Now you have to move insertMonday() into loop in controller:

$this->load->model('emp_model');
foreach($doctornames as $index => $doctorname){

    $doctorfield = $doctorfields[$index];
    $date = $dates1[$index] . '-' . $dates11[$index];

    $row = array(
        'doctorname' => $doctorname,
        'doctorfield' => $doctorfield,
        'schedule' => $date
    );

    $this->emp_model->insertMonday($row);

}

Hi, You’re the best. It is now working. Thank you :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.