CodeIgniter Forums
if exist update data else insert new data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: if exist update data else insert new data (/thread-76452.html)



if exist update data else insert new data - eleumas - 05-14-2020

I have a table with images. The user can update the existing images or add new. After he can save and send all data with POST. I would like update the data for existing images and insert new rows if the images doesn't exist. What is the correct query for to do this in Codeigniter 4?

Thanks for the support.


RE: if exist update data else insert new data - InsiteFX - 05-14-2020

Try to fetch the image if it does not exist then an empty array will be returned.

Now you can use and if statement to check the returned array,
if empty insert image else update image.


RE: if exist update data else insert new data - eleumas - 05-14-2020

(05-14-2020, 04:25 AM)InsiteFX Wrote: Try to fetch the image if it does not exist then an empty array will be returned.

Now you can use and if statement to check the returned array,
if empty insert image else update image.

Thanks for your answer. If i have 5 images: 3 updated and 2 new...with a if statement i can update 3 images OR add 2 new images. Have i lost something in your answer?  Undecided