CodeIgniter Forums
How to Edit data? Simple question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to Edit data? Simple question (/showthread.php?tid=72563)



How to Edit data? Simple question - Razzer - 01-06-2019

Hello everyone! I have a simple question about editing data in CI.

I have a simple view, with link to edit a record, like this:


PHP Code:
<?php echo site_url("edit/person/".$item['id']) ;?>


where 'id' is user ID in a database.

On this View, I have a form:


PHP Code:
<?php echo form_open('edit/person'); ?>



There is my Edit method:
PHP Code:
public function person($id=NULL)
 
   {
 
       $this->form_validation->set_rules('username''User Name''required');

 
           if ($this->form_validation->run() === FALSE) {
 
               
                $data
['person'] = $this->table_model->get_person($id);
 
               $this->load->helper('url');
 
               $this->load->view('template/header');
 
               $this->load->view('detail'$data);
 
               $this->load->view('template/footer');
 
           }
 
           else
            
{
 
               $data['message']='All changes saved';
 
               $this->table_model->person_edit();
 
               $this->load->view('template/header');
 
               $this->load->view('success',$data);
 
               $this->load->view('template/footer');
 
           }
 
       


And my model has 'person_edit()' method. This code is working fine and updates the data, but if I try to submit form with blank 'username' field, I get an error because 
PHP Code:
$this->load->view('detail'$data); 


in my Edit method doesn't have parameter for ID.

How can I put the GET ID  parameter in a load-view? thanks

Sorry for a noob question...


RE: How to Edit data? Simple question - InsiteFX - 01-06-2019

Your getting the error because you are using a set_rule on the username field.

If your editing a record you do not need the set_rule for the username.


RE: How to Edit data? Simple question - Razzer - 01-06-2019

Not working. I think I getting the error because there is no ID in GET parameter, after I clear "username" field and click "submit" button.

my "edit" method, id is "hidden" form field:
PHP Code:
public function person_edit()
    {
        $wh=array('id'=>$this->input->post('id'));
        $data = array(
            'person_name'=> $this->input->post('username'),
            'person_d' => $this->input->post('d'),
            'person_department' => $this->input->post('department'),
            'city_id' => $this->input->post('address')

        );

        return $this->db->update('users'$data,$wh);
    }  



RE: How to Edit data? Simple question - ciadmin - 01-06-2019

See https://www.codeigniter.com/user_guide/database/query_builder.html?highlight=db%20update#CI_DB_query_builder::update

Your
Code:
$this->db->update('users', $data,$wh);
will ignore $wh, since $data is an array.

Instead, add the id to the data array:
Code:
       $data = array(
           'id' => $this->input->post('id');
           'person_name'=> $this->input->post('username'),
           'person_d' => $this->input->post('d'),
           'person_department' => $this->input->post('department'),
           'city_id' => $this->input->post('address')

       );



RE: How to Edit data? Simple question - Razzer - 01-06-2019

Not working. Update method is working fine, if the "username" field is not empty. Hm... Let's watch an example: I have a link http://localhost/it/index.php/edit/person/28, where "28" is person ID in database. If "username" field is not empty, update method correctly update table for user_id 28. If I try to submit form with empty "username" field, I have a link http://localhost/it/index.php/edit/person/, without ID parameter. ID is needed to get data from database. That's why I have en error.

How can I add ID parameter to the view, after I click "submit" button? And my route after submitting form http://localhost/it/index.php/edit/person/28, not http://localhost/it/index.php/edit/person


RE: How to Edit data? Simple question - InsiteFX - 01-07-2019

Then you will need to check if the username is empty or not.

PHP Code:
$data = array(
 
   if (! empty($this->input->post('username')))
 
   {
 
       'person_name'=> $this->input->post('username'),
 
   }
 
   'person_d' => $this->input->post('d'),
 
   'person_department' => $this->input->post('department'),
 
   'city_id' => $this->input->post('address')
); 

Other wise you are passing the person_name to the database as empty.

A better way of doing this would be to fill in all of the form fields on an edit
then allow them to change the fields.