scaryguy
scaryguy

Reputation: 7960

Undefined index error... Adding new indexes to an existing array

I've spent almost 2 days with this error.

if ($_FILES['userfile']['error'] !== 4) {

                $this -> load -> library('upload');

                $settings = array('upload_path' => '././images/yeniler', 'allowed_types' => 'jpg|jpeg');

                $this -> upload -> initialize($settings);

                $dosya = $this -> upload -> do_upload();
                $upload_data = $this -> upload -> data('userfile');
                $formverileri[]="";
                if ($dosya) {
                $dosyaadi = $upload_data['file_name'];

                }

            }
            $formverileri['yeniler_resim'] =$dosyaadi; 
            $formverileri = array('yeniler_baslik' => $this -> input -> post('yeniler_baslik'), 'yeniler_detay' => $this -> input -> post('yeniler_detay'));

            if ($guncelle = $this -> yeniler_model -> updateData($formverileri)) {
                var_dump($formverileri);
                var_dump($upload_data);
                die;
                echo("başarılı");
                $this -> session -> set_flashdata("sonuc", "oldu");
                redirect(base_url() . "admin_yeniler/duzenle/" . $this -> input -> post('yeniler_id'));

            } else {
                echo("olmadı..");
            }

The problem is, I can not assing value of $upload_data['file_name'] into $formverileri['yeniler_resim'] ... When I check array's seperatly with print_r, I see exactly correct indexes and values. But I can NOT add $formverileri['yeniler_resim'] to my updating array...

Edit: ONLY IF file is UPLOADED I want to assing $upload_data['file_name'] to $formverileri['yeniler_resim']. I could duplicate my update function (which is $this->yeniler_model->updateData($formverileri)) but that wouldn't be a good practise. Just why I can't add new index to an existing array in a certain condition??

Upvotes: 0

Views: 134

Answers (1)

safarov
safarov

Reputation: 7804

When you define new array like $formverileri = array(.. you older value of that variable is replaced by new araay that u defined.

Try this code

 if ($dosya) {
            $dosyaadi = $upload_data['file_name'];
            $formverileri['yeniler_resim'] = $dosyaadi; 
            }

        }

        $formverileri['yeniler_baslik'] = $this -> input -> post('yeniler_baslik');
        $formverileri['yeniler_detay'] = $this -> input -> post('yeniler_detay');

Upvotes: 1

Related Questions