phpcodeigniteractiverecordsql-updatequery-builder

Update the qualifying column value in a qualifying row without knowing the column name


I want to get a single row from db, remove one element from row and update that row back to db.

I wrote this code

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

This is row

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] =>
)

I just want to remove ccode_1 (this is not fixed depends upon $ccode value) value and update it back to db, but it's not showing desired result as above.


Solution

  • You don't need to unset the desired index just get that index and apply update

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