• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Edit data? Simple question

#1
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...
Reply

#2
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
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);
    }  
Reply

#4
See https://www.codeigniter.com/user_guide/d...er::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')

       );
Reply

#5
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
Reply

#6
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.