【问题标题】:Error: Unknown column 'Array' in 'where clause'错误:“where 子句”中的未知列“数组”
【发布时间】:2011-12-05 06:19:58
【问题描述】:

我想用foreach 更新数据库表中的几行,但出现以下错误:

这是我的 php 代码:

$id_units       = $this->input->post('id_units');
$name_un       = $this->input->post('name_units');      
$price_un        = $this->input->post('price_units');
$description_un = $this->input->post('explanation_units');
$ex_un       = $this->input->post('addition_units');
$service_un        = $this->input->post('checkbox_units');
$data2 = array();
foreach ($name_un as $idx => $name) {
    $data2 = array(
        'relation' => $id_residence,
        'name_un' => $name_un[$idx],
        'price_un' => $price_un[$idx],
        'description_un' => $description_un[$idx],
        'ex_un' => $ex_un[$idx],
        'service_un' => json_encode($service_un[$idx]), // This Is Line Number: 212
    );
$this->db->update('hotel_units', $data2, array('id' => $id_units[$idx]));
};

上面的代码有这个错误:

发生数据库错误
错误号:1054
未知列 'where 子句'中的'数组'
UPDATE hotel_units SET 0 = 数组 WHERE id = 数组
文件名: D:\xampp\htdocs\system\database\DB_driver.php
行号:330

更新:

我有新的错误:

遇到 PHP 错误
严重性:通知
消息: 未定义的偏移量:1
文件名:residence.php
行号: 212

遇到 PHP 错误
严重性:通知
消息:未定义的偏移量:2
文件名:residence.php
行 号码:212

遇到 PHP 错误
严重性: 注意
消息:未定义的偏移量:3
文件名: Residence.php
行号:212

'service_un' =>... 是复选框。请参阅上述 php 代码中的行号:212

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:

    看起来$id_units 是一个值数组。

    您可以尝试使用此代码,但它假定与您的其他输入字段一样,id 也是一个数组,并且具有与其他字段相同的索引。您没有提供您的 html 表单,所以我只是猜测....无论如何, $this->db->where() 的最后一个参数必须是单个值,即使您将其写为数组(但只有一个元素)。

    $id_units       = $this->input->post('id_units');
    $name_un       = $this->input->post('name_units');      
    $price_un        = $this->input->post('price_units');
    $description_un = $this->input->post('explanation_units');
    $ex_un       = $this->input->post('addition_units');
    $service_un        = $this->input->post('checkbox_units');
    $data2 = array();
    foreach ($name_un as $idx => $name) {
        $data2 = array(
            'relation' => $id_residence,  // Or is it $id_residence[$idx] ?
            'name_un' => $name_un[$idx],
            'price_un' => $price_un[$idx],
            'description_un' => $description_un[$idx],
            'ex_un' => $ex_un[$idx],
            'service_un' => json_encode($service_un[$idx]),
        );
       $this->db->update('hotel_units', $data2, array('id' => $id_units[$idx]));
    };
    

    【讨论】:

    • 是的,它是数组,但我现在有这个错误:Unknown column '0' in 'field list', UPDATE 'hotel_units' SET '0' = Array WHERE 'id' = '32'
    • @Alicia Cibrian 知道了,我更正了我的答案:应该是$data2,而不是$data2[],我之前没看到
    • 你能发布你的html表单吗?否则,这将只是我的猜测游戏...如果您可以发布表单的结构,您甚至会从其他人那里获得更多帮助
    猜你喜欢
    • 2012-04-25
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多