CodeIgniter Forums
Best practice for editing db data. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: Best practice for editing db data. (/thread-71523.html)



Best practice for editing db data. - HarrysR - 08-23-2018

My question is mainly for educational purposes, so..
What is the best practice for updating data in db only if they are not empty in form?

For example lets say i have a form in user's panel which contains his info (e.g. user email, name etc..)
The fields are fulfilled with parsing data in "value" attribute, but the password and some other fields are empty by default. 

What is the fastest way to pass them into an array only if they are not blank, in order not to change the already registered value in db?!

I hope my question is understandable.. if not, feel free to ask for clarification!

Thank you!!


RE: Best practice for editing db data. - jreklund - 08-23-2018

Personally I query the DB for that user profile to get a $user object.
And then I do something like this:
PHP Code:
// Do your validation here or in form_validation.
if( !empty($this->input->post('address') ) {
 
   $user->address $this->input->post('address');

After that I do an update query to the DB. And it "replaces" everything again with new and old data.