php - Codeigniter 中仅更新一行

标签 php mysql codeigniter

下面是我的模型代码,当我尝试更新它时,它只更新第一行,而不更新其他行。我也尝试使用 update_batch,但它返回数据库错误页面。

   My Model Code:
      foreach ($ingredients as $item) {
        //  $this->sma->print_arrays($item);
       if($item['id']){
       $this->db->where('id', $item['id']);
       $this->db->update('items', $item);
       // }
       return true;
     }

我的阵列:(成分)

  Array
   (
    [0] => Array
    (
        [id] => 16
        [quantity] => 76
    )

[1] => Array
    (
        [id] => 92
        [quantity] => 97
    )

  )

我们将感谢您的帮助

最佳答案

使用update_batch更新一组数据。

$data = array(
    [0] => Array
    (
        [id] => 16
        [quantity] => 76
    )

    [1] => Array
    (
        [id] => 92
        [quantity] => 97
    )
);

$this->db->update_batch('tableName', $data, 'id');

关于php - Codeigniter 中仅更新一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43364878/

相关文章:

php - 导航按钮超出了 div

php - 如何在 Laravel 中使用 PUT http 动词提交表单

PhpMygraph - 如何从数据库 mysql 分配数组

php - Codeigniter 3 创建动态方法

php - 如何在codeigniter中使用参数为url创建分页?

php - 从 ZIP 字符串中提取文件

php - 带变量的 Html <A> 链接

mysql平均最新5行

php - 如何在 PHP 中获取 session 名称?

php - Ajax 调用 Controller 中的 php 函数