我在数据库中添加多行。
我在codeigniter中使用insert_batch
。
我看到了一些问题,但没有得到我做错的事。
我使用foreach
循环创建了一个数组,但是出现了这个错误。
这是foreach
循环:
foreach($add as $data){
$new_add[] = array(
'col1'=>$data['col1'],
'col2'=>$data['col2'],
'col3'=>$data['col3'],
'col4'=>$data['col4'],
'col5'=>$data['col5'],
'col6'=>$data['col6']
);
}
After Loop这是我得到的数组:
Array
(
[0] => Array
(
[col1] => col1_data
[col2] => col2_data
[col3] => col3_data
[col4] => col4_data
[col5] => col5_data
[col6] => col6_data
)
[1] => Array
(
[col1] => col1_data
[col2] => col2_data
[col3] => col3_data
[col4] => col4_data
[col5] => col5_data
[col6] => col6_data
)
)
插入批量查询:
$this->db->insert_batch('test', $new_add);
感谢此评论我得到了我做错的事。 comment 我在这里做了一个基本错误我想使用
insert_batch
但不知何故我忘了在查询中添加它。
错误:
遇到PHP错误
严重性:注意
消息:数组到字符串转换
文件名:database / DB_driver.php
行号:1476
回溯:
文件: d:\选择\ WAMP \ WWW \ yo_builder \程序\ \控制器Instalment.php 行:85功能:插入
文件:D:\ opt \ wamp \ www \ yo_builder \ index.php行:315功能: require_once
这是第二次错误。
错误号码:1064
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 在第1行附近'0,1)VALUES(数组,数组)
INSERT INTO
test
(0,1)VALUES(Array,Array)文件名:D:/opt/wamp/www/yo_builder/system/database/DB_driver.php
行号:691
答案 0 :(得分:0)
尝试像这样创建数组:
$insertArray = array();
foreach($add as $data){
$new_add = array(
'col1'=>$data['col1'],
'col2'=>$data['col2'],
'col3'=>$data['col3'],
'col4'=>$data['col4'],
'col5'=>$data['col5'],
'col6'=>$data['col6']
);
array_push($insertArray,$new_add);
}
然后像这样调用insert_batch
:
$this->db->insert_batch('test', $insertArray);
请确认" col1"," col2" ...是表的有效列名" test"。