doujiang2641 2013-02-01 00:59
浏览 43
已采纳

CodeIgniter:使用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

  • 写回答

1条回答 默认 最新

  • dongliantong3229 2013-02-01 01:35
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办