【问题标题】:Insert several array of value different in new row from database在数据库的新行中插入几个不同的值数组
【发布时间】:2011-11-22 00:29:17
【问题描述】:

我想在数据库的行中插入值数组,因为它们每个都从数据库中放入新行。

我不想使用序列化(json_encode 等)从数据库中连续插入所有值。

例如:(在这个例子中,我要插入 3 次数据(值),因为我有 3 个不同的值) 更新 4:

这是我的价值:

<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">

我做了,但没用:

$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);

这是带有var_dump 的输出'$name_input':

array(2) {
    [0] = > array(1) {
        [0] = > string(2)"11"
    }[1] = > array(1) {
        [0] = > string(2)"22"
    }
}

我收到此错误:

遇到 PHP 错误
严重性:警告
消息:不能 修改标头信息 - 已发送的标头(输出开始于 D:\xampp\htdocs\application\controllers\admin\tour_foreign.php:405)
文件名:core/Common.php
行号:413

数据库 发生错误
错误号:1054
“字段”中的未知列“0” 列表'
插入 customer_info (0) 值 ('22')
文件名:D:\xampp\htdocs\system\database\DB_driver.php
行 数量:330

【问题讨论】:

    标签: php arrays codeigniter for-loop


    【解决方案1】:

    更新 3:每个更新的问题(更新 4)
    查看$name_inputvar_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);
    

    当 POST 输入作为 array of arrays 传递时,还需要访问 [0] 元素

    更新 2: 看到问题是因为 POST 将数据作为代码可能工作之后的另一个数组返回。我需要var_dump 的 POST 输入 ($hi_input..) 才能给出准确的代码。

    $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);
    

    我已经测试过以下代码应该可以工作(我没有安装 CodeIgniter)。它不使用 CodeIgniter 来获取 post 数据。

    $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);
    

    更新:
    您也可以使用$this-&gt;db-&gt;insert_batch(); 执行此操作,如下所示:

    $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);
    

    旧答案
    关于插入的 CodeIgniter 文档 - http://codeigniter.com/user_guide/database/active_record.html#insert
    声明 $data 参数是一个关联数组,列名作为键,数据作为值插入。

    所以你需要为每一行调用一次。因此
    试试这个:

    $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);
    };
    

    【讨论】:

    • 请看我的帖子更新,insert_batch 有同样的错误,如何解决?
    • @EmmyCharles 您能否在 te 数组之后输出 $data 数组的内容,并检查其中的内容。我的代码中还有一个小错误insert_batch。我已经更新了我的答案。您发布的错误还表明这是因为insert 正在尝试将数组转换为字符串,并且密钥被视为0。您是否尝试过将插入插入到 foreach 中,就像我的第二个代码示例一样?
    • 好的,我改变了它,我得到了新的错误。您可以在我的第一篇文章中看到新的错误和输出代码。你的评论是什么?
    • @EmmyCharles 似乎post 正在返回一个数组来代替字符串。我需要给你的最后一条信息是$hi_input$hello_input$how_input的工作代码idprint_r。但如果没有它,也可能在 foreach 中引用这样的索引 $hi_input[$idx][1] 可能会起作用。
    • 它不起作用。您对以下代码的使用有何评论:(如何在我的代码中使用它)$sql = array(); foreach( $data as $row ) { $sql[] = '("'.mysql_real_escape_string($row['text']).'", '.$row['category_id'].')'; } mysql_query('INSERT INTO table (text, category) VALUES '.implode(',', $sql)); 请参见此处的代码:jsfiddle.net/2NJy8
    猜你喜欢
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 2021-10-06
    • 2022-01-24
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多