Himanshu Yadav
Himanshu Yadav

Reputation: 13585

CodeIgniter: Getting error while inserting an array with Active Records

Trying to insert an array with the help of CodeIgniter Active reacords. Table has just two columns id(autoincrement pk column) and value.

$data array I am populating from Google Contact API response. Here how I am populating array $data in controller and passing it to model

$val = $client->getIo()->authenticatedRequest($req);//Getting response from Google Contact API.
$xml = simplexml_load_string($val->getResponseBody());
$result = $xml->xpath('//gd:email');  //Fetching email addressed from the response
foreach ($result as $title) {
            array_push($gmailContacts, mysql_real_escape_string($title->attributes()->address));
 }
$this->load->model('gmailContacts');
$this->gmailContacts->saveContacts($gmailContacts);


Model Code is

  function saveGmailContacts($data=array()) 
  {
    $this->db->insert('contact_table',$data);
  }

Error is

Error Number: 1054

Unknown column '0' in 'field list'

INSERT INTO `importedgmailcontacts` (`0`, `1`, `2`, `3`, `4`, `5`)VALUES ('VALUE1', 'VALUE2', 'VALUE3','VALUE4', 'VALUE5') 

I am using Codeigniter with XAMPP 1.7.7 which has PHP 5.3.3 and MySql 5.0

Upvotes: 0

Views: 438

Answers (1)

Jerry
Jerry

Reputation: 3608

CodeIgniter assumes when you pass an array like that that you are setting a single row with multiple columns. The array keys are the column names and the values are what goes into the row. You will need to call insert multiple times, once for each row, if you want to use the ActiveRecord class.

You will likely be better off rolling your own query for this one.

Upvotes: 2

Related Questions