简体   繁体   English

无法在Codeigniter中更新数据库查询

[英]Unable to update database query in Codeigniter

I have to fetch the record from the form of an array. 我必须从数组的形式中获取记录。 following is an array result. 以下是数组结果。

Array(
    [price] => Array
    (
        [0] => 210
    )

    [code] => Array
    (
        [0] => SER-1001
    )
)
Array
(
    [price] => Array
    (
        [1] => 80
    )

    [code] => Array
    (
        [1] => XYZ-121
    )
)

Now I am confused, how to update that records in product table where a code is eg SER-1001 in the database then the price should be updated 210 . 现在我很困惑,如何更新产品表中的记录,其中数据库中的代码是例如SER-1001 ,那么价格应该更新210

Same way as I do rest of matching code records. 与我执行其余匹配代码记录的方式相同。

foreach ($arr as $key => $value) {
  $data = array(  
    'price'=> $value['price'];
  );  
  $this->db->where('code', $value['code']);  
  $this->db->update(‘database-table-name’,$data);   
}

try this, here you are looping through the array and updating the data one by one. 试试看,这里您正在遍历数组并一步一步地更新数据。

    $array = [
     [
        'price' => [ '0' => 210],
        'code' =>['0' => 'SER-1001']
    ],
    [
        'price' => ['1' => 80],
        'code' =>['1' => 'XYZ-121']
    ]
];

With reference to the above output array. 参考上面的输出数组。 You can write the code something like this. 您可以这样编写代码。


$counter = 0;
foreach($array as $data)
{
    if(isset($data['code'][$counter]))
    {
        $code = $data['code'][$counter];

        $data = [
            'price' => $data['price'][$counter];
        ];

        $this->db->where($column, $code);

        $this->db->update('database-table-name',$data);  
    }

    $counter++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM