[英]Insert several array of value different in new row from database
I want insert in the row from database, array of values as that each of they put in new row from database. 我想从数据库的行中插入值数组,因为它们每个都从数据库中插入新行。
I not want insert all values in a row from database with use of serialize(json_encode or etc). 我不想使用serialize(json_encode等)将所有值从数据库中插入一行。
For example: (in this example, i want three times insert data(value), because i have 3 value different) Update 4: 例如:(在此示例中,我要三次插入数据(值),因为我有3个不同的值) 更新4:
this my value: 这是我的价值:
<input name="name[0][]" value="11">
<input name="passport_number[0][]" value="11">
<input name="name[1][]" value="22">
<input name="passport_number[1][]" value="22">
i do it, but it not worked: 我做到了,但是没有用:
$name_input = $this->input->post('name');
$passport_number_input = $this->input->post('passport_number');
//$term_passport_input = $this->input->post('term_passport');
//$date_of_birth_input = $this->input->post('date_of_birth');
//$age_input = $this->input->post('age');
//$national_number_input = $this->input->post('national_number');
//$mobile_input = $this->input->post('mobile');
//$address_input = $this->input->post('address');
$data = array();
foreach ($name_input as $idx => $name) {
$data[] = array(
'name' => $name_input[0]
'passport_number' => $passport_number_input[0],
//'term_passport' => $term_passport_input[$idx],
//'date_of_birth' => $date_of_birth_input[$idx],
//'age' => $age_input[$idx],
//'national_number' => $national_number_input[$idx],
//'mobile' => $mobile_input[$idx],
//'address' => $address_input[$idx],
);
};
var_dump($name_input);
$this->db->insert_batch('customer_info', $data);
This is output '$name_input' with var_dump
: 这是输出'$ name_input'与var_dump
:
array(2) {
[0] = > array(1) {
[0] = > string(2)"11"
}[1] = > array(1) {
[0] = > string(2)"22"
}
}
I get this error: 我收到此错误:
A PHP Error was encountered 遇到PHP错误
Severity: Warning 严重程度:警告
Message: Cannot modify header information - headers already sent by (output started at D:\\xampp\\htdocs\\application\\controllers\\admin\\tour_foreign.php:405) 消息:无法修改标题信息-已发送的标题(输出从D:\\ xampp \\ htdocs \\ application \\ controllers \\ admin \\ tour_foreign.php:405开始)
Filename: core/Common.php 文件名:core / Common.php
Line Number: 413 行号:413A Database Error Occurred 发生数据库错误
Error Number: 1054 错误编号:1054
Unknown column '0' in 'field list' “字段列表”中的未知列“ 0”
INSERT INTOcustomer_info
(0
) VALUES ('22') 插入到customer_info
(0
)值('22')中
Filename: D:\\xampp\\htdocs\\system\\database\\DB_driver.php 文件名:D:\\ xampp \\ htdocs \\ system \\ database \\ DB_driver.php
Line Number: 330 行号:330
Update 3: Per updated question (update 4) 更新3:每个更新的问题(更新4)
Looking at the var_dump
of $name_input
following code should work: 查看$name_input
的var_dump
,以下代码应该可以工作:
$name_input = $this->input->post('name');
$passport_number_input = $this->input->post('passport_number');
$data = array();
foreach ($name_input as $idx => $name) {
$data[] = array(
'name' => $name_input[$idx][0],
'passport_number' => $passport_number_input[$idx][0]
);
};
$this->db->insert_batch('customer_info', $data);
It is further needed to access the [0] element as the POST input is passes as an array of arrays
当POST输入作为array of arrays
传递时,还需要访问[0]元素
Update 2: Seeing the problem is because the POST returns the data as a further array following code MIGHT work. 更新2:看到问题是因为POST在代码MIGHT工作之后将数据作为另一个数组返回。 I would need var_dump
of POST inputs ( $hi_input
..) to give the exact code. 我需要POST输入( $hi_input
..)的var_dump
来提供确切的代码。
$hi_input = $this->input->post('hi');
$hello_input = $this->input->post('hello');
$how_input = $this->input->post('how');
$data = array();
foreach ($hi_input as $idx => $name) {
$data[] = array(
'hi' => $hi_input[$idx][0],
'hello' => $hello_input[$idx][0],
'how' => $how_input[$idx][0]
);
};
$this->db->insert_batch('table', $data);
The following code should work as I have tested it (I do not have CodeIgniter Installed). 以下代码在我测试过后应该可以工作(我没有安装CodeIgniter)。 It does not use CodeIgniter to get post data. 它不使用CodeIgniter获取帖子数据。
$hi_input = $_POST['hi'];
$hello_input = $_POST['hello'];
$how_input = $_POST['how'];
$data = array();
foreach ($hi_input as $idx => $name) {
$data[] = array(
'hi' => $hi_input[$idx][0],
'hello' => $hello_input[$idx][0],
'how' => $how_input[$idx][0]
);
};
$this->db->insert_batch('table', $data);
Update : 更新:
You can also do this using $this->db->insert_batch();
您也可以使用$this->db->insert_batch();
来完成此操作$this->db->insert_batch();
, like this : , 像这样 :
$hi_input = $this->input->post('hi');
$hello_input = $this->input->post('hello');
$how_input = $this->input->post('how');
$data = array();
foreach ($hi_input as $idx => $name) {
$data[] = array(
'hi' => $hi_input[$idx],
'hello' => $hello_input[$idx],
'how' => $how_input[$idx]
);
};
$this->db->insert_batch('table', $data);
Old answer 旧答案
The CodeIgniter documentation on insert - http://codeigniter.com/user_guide/database/active_record.html#insert 有关插入的CodeIgniter文档-http: //codeigniter.com/user_guide/database/active_record.html#insert
states that the $data parameter is an associative array with column names as keys and data to insert as values. 指出$ data参数是一个关联数组,列名作为键,数据作为值插入。
So you need to call it once for each row. 因此,您需要为每行调用一次。 Thus 从而
Try this: 尝试这个:
$hi_input = $this->input->post('hi');
$hello_input = $this->input->post('hello');
$how_input = $this->input->post('how');
foreach ($hi_input as $idx => $name) {
$data = array(
'hi' => $hi_input[$idx],
'hello' => $hello_input[$idx],
'how' => $how_input[$idx]
);
$this->db->insert('table', $data);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.