【问题标题】:get a single row from db, remove one element from row and update that row back to db从 db 中获取一行,从行中删除一个元素并将该行更新回 db
【发布时间】:2015-03-14 09:53:03
【问题描述】:

我想从 db 中获取一行,从行中删除一个元素并将该行更新回 db。

我写了这段代码

$query = $this->db->get_where(TABLENAME, array('pin_code' => $pincode), 1);

$result = $query->result_array();

$array = $result[0]; 

if (($key = array_search($ccode, $array)) !== false) {
            unset($array[$key]);
} //$code is the value which i want to remove

$this->db->where('pin_code', $pincode);
$this->db->update(TABLENAME, $array); 

这是行

Array ( [pin_code] => 854101 [ccode_1] => 5806 [ccode_2] => [ccode_3] => [ccode_4] => [ccode_5] => [ccode_6] => [ccode_7] => [ccode_8] => [ccode_9] => [ccode_10] => [ccode_11] => [ccode_12] => [ccode_13] => [ccode_14] => [ccode_15] => [ccode_16] => [ccode_17] => [ccode_18] => [ccode_19] => [ccode_20] => [ccode_21] => [ccode_22] => [ccode_23] => [ccode_24] => [ccode_25] => [ccode_26] => [ccode_27] => [ccode_28] => [ccode_29] => [ccode_30] => ) 

我只想删除 ccode_1(这不是固定的,取决于 $ccode 值)值并将其更新回 db。

但它没有显示如上所需的结果。

【问题讨论】:

  • 您需要多解释一下您想要实现的目标,提供一些示例数据集可能会有所帮助
  • 这是行数组 ( [pin_code] => 854101 [ccode_1] => 5806 [ccode_2] => [ccode_3] => [ccode_4] => [ccode_5] => [ccode_6] => [ccode_7] => [ccode_8] => [ccode_9] => [ccode_10] => [ccode_11] => [ccode_12] => [ccode_13] => [ccode_14] => [ccode_15] => [ccode_16] => [ccode_17] => [ccode_18] => [ccode_19] => [ccode_20] => [ccode_21] => [ccode_22] => [ccode_23] => [ccode_24] => [ccode_25] => [ccode_26] => [ccode_27] => [ccode_28] => [ccode_29] => [ccode_30] => )
  • 这是我的数据库图片postimg.org/image/puia0qatn

标签: mysql arrays codeigniter activerecord


【解决方案1】:

您不需要取消设置所需的索引,只需获取该索引并应用更新

if (($key = array_search($ccode, $array)) !== false) {
$this->db->where('pin_code', $pincode);
$this->db->update(TABLENAME, array($key => ''));
} 

【讨论】:

  • @Kiranarya print_r($key);在 if 检查里面看看输出是什么或检查它是否真的进入 if 部分?并确保你在 $ccode/$array 中有一个值
猜你喜欢
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多