duanqiao2006 2011-09-21 14:21
浏览 44
已采纳

在数据库的新行中插入几个不同的值数组

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).

For example:(in this example, i want three times insert data(value), because i have 3 value different) Update 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:

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
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\application\controllers\admin\tour_foreign.php:405)
Filename: core/Common.php
Line Number: 413

A Database Error Occurred
Error Number: 1054
Unknown column '0' in 'field list'
INSERT INTO customer_info (0) VALUES ('22')
Filename: D:\xampp\htdocs\system\database\DB_driver.php
Line Number: 330

  • 写回答

1条回答 默认 最新

  • douyue3800 2011-09-21 14:25
    关注

    Update 3: Per updated question (update 4)
    Looking at the var_dump of $name_input following code should work:

    $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

    Update 2: Seeing the problem is because the POST returns the data as a further array following code MIGHT work. I would need var_dump of POST inputs ($hi_input..) to give the exact code.

    $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). It does not use CodeIgniter to get post data.

    $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();, 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
    states that the $data parameter is an associative array with column names as keys and data to insert as values.

    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);
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog