Tuesday 15 June 2010

php - How to leave value unchanged if left empty in form Codeigniter -


I have an update with file input to add an image What am I trying to achieve, when I The field will leave blank, then the file will not be changed. Therefore, the photo I uploaded earlier will be there.

My formulas are called: & lt; Td> & Lt ;? = Form_upload ('aanbiedingfoto') ;? & Gt; & Lt; / Td>

And my database field is called Photonam .

I tried to do something like that, but it does not work: if ($ _FILES ['aanbiedingfoto'] ['name']! = '') {$ Data ['photonam' ] = $ _FILES ['ANBIDING PHOTO'] ['NAME']; }

What am I doing?

My Controller: function editaanbieding () {$ data = array ('Aanbieding' = & gt; it-> Input-> Post ('ANBIDINGNAM'), 'TECH' => $ -> THIS- & gt; INPUT- & gt; POST ('aanbiedingomschrijving'), 'Praj' => $ -> Input- & gt; Post ('ANBidingProgress'), 'Conductivity' => $ -> IT-> Input-> Post ('ANBIDINGCandity'); If ($ _ files ['ANBIDING PHOTO'] ['NAME']! = '') {$ Data ['phototonym'] = $ _FILES ['ANBIDING PHOTO'] ['NAME']; } Print_r ($ _ files); $ Config ['upload_path'] = './sets/uploads/'; $ Config ['allowed_types'] =' GIF | JPG | PNG '; $ Config ['max_size'] = '1000'; $ Config ['max_width'] = ''; $ Config ['max_height'] = ''; $ Config ['overwrite'] = true; $ Config ['remove_spaces'] = true; $ Config ['file_name'] = $ this- & gt; Input-> Post ('aanbiedingfoto'); $ This- & gt; Load-> Library ('upload', $ config); If (! $ This-> upload-> do_upload ('aanbiedingfoto')) {$ error = array ('error' => $ this-> upload-> display_errors ()); } And {$ image_data = $ this- & gt; Upload-> Data (); } $ This- & gt; Aanbieding_model- & gt; Edit_aanbieding ($ data, $ image_data); Redirect ('members / aanbiedingen'); }

My model:

  function edit_aanbieding ($ data, $ image_data) {$ id = $ this- & gt; Uri-> Segment (3); $ Id2 = $ this- & gt; Input-> Post ('phototoid'); $ Id3 = $ this- & gt; Input-> Post ('aanbiedingid'); / * Echo 'badgefanbailing', $ id, '& lt; Br / & gt; '; Echo 'phototode', $ id2, '& lt; Br / & gt; '; Echo 'aanbiedingid', $ id3, '& lt; Br / & gt; '; Die; * / $ This- & gt; Db- & gt; Where ('idanbiedingen', $ id3); $ This- & gt; Db- & gt; Update ('ANBIDENGEN', $ data); $ This- & gt; Db- & gt; Where ('idfotoaanbiedingen', $ id2); $ Insertfoto = array ('fotonaam' = & gt; $ image_data ['file_name']); $ This- & gt; Db- & gt; Update ('photianabidin', $ insertfoto); $ This- & gt; Db- & gt; Where ('idbedrijfaanbiedingen', $ id); } If you do not upload a file, then  $ image_data  

will NULL .

Though there are better solutions, you can simplify it as follows:
Find this section in your model and change it below:

  if (isset ($ Image_data)) {$ insertfoto = array ('fotonaam' = & gt; $ image_data ['file_name']); $ This- & gt; Db- & gt; where (...); // Right logic $ this-> Db- & gt; Fill in the blank with the update ('fotoaanbiedingen', $ insertfoto); }   

Side note:

It is completely optional to increase performance and to get rid of potential errors, I give the following suggestions:

  • Avoid libraries or calling methods not necessary.
  • Set the default value for the function arguments.

    Just as a demo:

    Controller:

      ... if (! Blank ($ _files ['ANBidingFoto'] ['name'])) {$ data ['phototenam'] = $ _FILES ['ANBIDING PHOTO'] ['NAME']; $ Config ['upload_path'] = './sets/uploads/'; $ Config ['allowed_types'] =' GIF | JPG | PNG '; $ Config ['max_size'] = '1000'; $ Config ['max_width'] = ''; $ Config ['max_height'] = ''; $ Config ['overwrite'] = true; $ Config ['remove_spaces'] = true; $ Config ['file_name'] = $ this- & gt; Input-> Post ('aanbiedingfoto'); $ This- & gt; Load-> Library ('upload', $ config); If ($ - this-> upload-> do_upload ('aanbiedingfoto')) {$ this- & gt; Aanbieding_model- & gt; Edit_aanbieding ($ data, $ this-> upload-> data ()); } And {$ error = array ('error' =>> $ this-> upload-> display_errors ()); // If you want to update the table, uploading $$ - $ a- as-gt; Aanbieding_model- & gt; Edit_aanbieding ($ data); }} ...   

    model:

      function editing_anbasing ($ data, $ image_data = NULL) {... If (isset ($ image_data)) {$ insertfoto = array ('fotonaam' = & gt; $ image_data ['file_name']); $ This- & gt; Db- & gt; where (...); // Right logic $ this-> Db- & gt; Fill in the blank with the update ('fotoaanbiedingen', $ insertfoto); } ...}    

No comments:

Post a Comment