-1

I am try to append my data array in if conditions. I am using array_push function to do it Its first time I am using this function for appending array. I am using conditions so that if user has added the value in the form it would update the field otherwise It would not effect the field. The problem is its not updating the database and its unable to set the fields as it shows Unknown Unknown column '0' in 'field list'

$fid = 2;
$password = "test_pass";
$title = "new title of folder";
$f_access = 1;
$newName = TRUE;

$data = array(
              'name' => $title,
              'access_type' => $f_access
              );

if($newName)
{
    $data2 = "'icon' => $newName";
    array_push($data, $data2);
}

if($password)
{
    $data3 =   "'password' => $password";
    array_push($data, $data3);
}

$this->db->where('id', $fid);
$this->db->update('folders', $data);
3
  • 2
    You want $data['icon'] = $newName; instead of push for this
    – ahmad
    Commented Apr 14, 2013 at 11:50
  • stock values in an new array for transition and use array_merge to preserve precedent indexes
    – e-Learner
    Commented Apr 14, 2013 at 11:56
  • 1
    Please read up on PHP arrays
    – kittycat
    Commented Apr 14, 2013 at 12:02

2 Answers 2

3

To insert new data into a key => value array you have to use the form:

 $arr['key'] = value;

as

 $data['password'] = $password;
0
$data = array(
          'name' => $title,
          'access_type' => $f_access
          );

if(!empty($newName))
    $data['icon'] = $newName;

if(!empty($password))
    $data['password'] = $password;

This way you can easily push new items in the array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.