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 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?