multiple value insert using saveField or other Function

Question

I insert multiple values into my database using saveField function. Here is my code.

$this->Fuel->saveField(
        'device_id', $fuel_func[0],
        'func_code', $fuel_func[1],
        'device_data', $fuel_func[2]
    );

It's not a form value. Value automatically insert when the page is refreshed.

It's only inserts first value "'device_id', $fuel_func[0]". Other two values are not insert into my database. Any one suggest me how to insert these values.

Thanks in advance

Cake version 2.7.5


Show source
| cakephp   | php   | mysql   2017-01-07 06:01 3 Answers

Answers ( 3 )

  1. 2017-01-07 08:01

    saveField function is only for updating one field! use updateAll and pass an array to it :

    $this->Fuel->updateAll(
            array(
             'device_id', $fuel_func[0],
            'func_code', $fuel_func[1],
            'device_data', $fuel_func[2] 
           ),
          array('Fuel.id' => 234) // conditions for the Fuel you want to update
    
        );
    
  2. 2017-01-07 11:01

    My id is not auto Increment it is defined as varchar. Time is not include there also. I got my answer. I use it and it works.

    $this->Fuel->set(array(
            'device_id'   => $int_fuel_value[0],
            'func_code'   => $int_fuel_value[1],
            'device_data' => $int_fuel_value[2]
        ));
    
        $this->Fuel->save();
    
  3. 2017-01-07 11:01

    You can simply do this as well.

    $data = array(
        'device_id'   => $int_fuel_value[0],
        'func_code'   => $int_fuel_value[1],
        'device_data' => $int_fuel_value[2]
      );
    
    $this->Fuel->save($data);
    

    Also try this blog tutorial (cakephp blog tutorial) if you haven't been there before to be more familiar with these things.

◀ Go back