owf
owf

Reputation: 251

Invalid array insert_batch() codeigniter

I'd like to insert this array data to db using insert_batch() of codeigniter

$data = Array ( 
   [0] => Array 
   ( 
      [e_purpose] => blank purpose data on row 2 
      [e_phase] => blank phase data on row 2 
      [e_carmaker] => please input a valid and related carmaker on row 2
      [e_carline] => blank carline data on row 2
      [table] => Expense Budget
      [row] => 2
      [section] => FINANCE 
   )
   [1] => Array 
   ( 
      [e_purpose] => blank purpose data on row 3 
      [e_phase] => please input a valid and related phase on row 3 
      [table] => Expense Budget 
      [row] => 3 
      [section] => FINANCE 
   )
)

and here's my insert query :

$this->db->insert_batch('t_error', $data);

and i've got this error :

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: database/DB_query_builder.php

Line Number: 1542

Backtrace:

File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\models\Excel_model.php
Line: 21
Function: insert_batch

File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\controllers\Excel.php
Line: 125
Function: insertError

File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\index.php
Line: 315
Function: require_once

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Array' at line 1

INSERT INTO `t_error` () VALUES ('blank carline data on row 2','please input a valid and related carmaker on row 2','blank phase data on row 2','blank purpose data on row 2',2,'FINANCE','Expense Budget'), Array

Filename: D:/ONNE/OTHERS/_CODING_PROGRAMMING/XAMPP/htdocs/bulus-ci/system/database/DB_driver.php

Line Number: 691

How can i fixed this, Note : i don't want to use loop/iteration

Upvotes: 0

Views: 248

Answers (1)

CuriousTeam
CuriousTeam

Reputation: 96

Though I have not tested it on my computer. But I guess, the problem is with column names. You have to send set same key in all arrays.

$data = array ( 
   array ( 
      'e_purpose' => 'blank purpose data on row 2', 
      'e_phase' => 'blank phase data on row 2', 
      'e_carmaker' => 'please input a valid and related carmaker on row 2',
      'e_carline' => 'blank carline data on row 2',
      'table' => 'Expense Budget',
      'row' => 2,
      'section' => 'FINANCE' 
   ),
   array ( 
      'e_purpose' => 'blank purpose data on row 3',
      'e_phase' => 'please input a valid and related phase on row 3',
      'e_carmaker' => '', //Send blank values'
      'e_carline' => '', //Send blank values'
      'table' => 'Expense Budget',
      'row' => 3,
      'section' => 'FINANCE',
   )
)

Let me know if works for you so that I also can learn the solutions.

Upvotes: 2

Related Questions