尝试在 CodeIgniter Active 记录的帮助下插入数组。表只有两列id(自动增量pk列)
和value
。
$data 数组是我从 Google Contact API 响应中填充的。这是我如何在 Controller 中填充数组 $data 并将其传递给模型
$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);
型号代码为
function saveGmailContacts($data=array())
{
$this->db->insert('contact_table',$data);
}
错误是
Error Number: 1054
Unknown column '0' in 'field list'
INSERT INTO `importedgmailcontacts` (`0`, `1`, `2`, `3`, `4`, `5`)VALUES ('VALUE1', 'VALUE2', 'VALUE3','VALUE4', 'VALUE5')
我正在将 Codeigniter 与 XAMPP 1.7.7 一起使用,其中包含 PHP 5.3.3 和 MySql 5.0
最佳答案
CodeIgniter 假设当您传递这样的数组时,您正在设置具有多列的单行。数组键是列名,值是行中的内容。如果您想使用 ActiveRecord 类,则需要多次调用 insert,每行调用一次。
您可能最好针对此查询自行进行查询。
关于php - CodeIgniter:插入带有事件记录的数组时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14638000/