0

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??

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

7 Comments

I can't do that because I need to check if upload is done or not. Thing I'm trying to do is: If an image is picked up and uploaded, do upload and set upload data. And give me file_name data to update yeniler_resim field in db.
Thanks for your update safarov. But what if $dosyaadi is not set? I don't want yeniler_resim field to be updated unless a file is uploaded.
Than use if(isset($dosyaadi) && strlen($dosyaadi)) { // do update} else {//dont do upload}
Read my edit please. Is it you who voted negative my question?
No it means when you use $var = array(.. you create new array. old $var is vanish
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.