3

I am using codeigniter for this project. I have not getting how to update form data in the database. I have inserting, showing data in the databse is done. But I can't understand, how to update data in the database.

My controller:

class User extends CI_Controller {

    public function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->load->model('usermodel');
    }

    public function insert() {
        $this->load->view('userview');

        if ($this->input->post('submit')) {

            $this->usermodel->save();
        }
    }

    public function display() {
        $data = array();
        $data['result'] = $this->usermodel->get_contents();
        $this->load->view('usergrid', $data);
    }

    public function edit() {
        $data = array();
        $get = $this->uri->uri_to_assoc();
        $data['result'] = $this->usermodel->entry_update( $get['id'] );

        $this->load->view('useredit', $data);
         if ($this->input->post('submit')) {

            $this->usermodel->entry_update1($get['id']);
        }
    }


}

model:

<?php

class Usermodel extends CI_Model {

    public function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    public function save() {

        //print_r($this->input->post('name'));

        $data = array(
            'name' => $this->input->post('name'),
            'age' => $this->input->post('age'),
            'address' => $this->input->post('address')
        );
        //var_dump($this->db);

        $this->db->insert('user', $data);
    }

    public function get_contents() {
        $this->db->select('*');
        $this->db->from('user');
        $query = $this->db->get();
        return $result = $query->result();
    }


    public function entry_update( $id ) {

        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('id',$id );
        $query = $this->db->get();
        return $result = $query->row_array();

    }
    public function entry_update1($id) {
      $data = array(

'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'address' => $this->input->post('address')
        );

         $this->db->where('id', $id);
        $this->db->update('user', $data);

    }

}
?>

view:

<html>
    <head>
        <title>user registration</title>
    </head>
    <body>
        <form  action="edit"  method="POST" name="myform">
            <input type="hidden" name="id" value="<?php echo $result['id']; ?>">
            username :<input type="text" name="name" value="<?php echo $result['name'] ?>"></br>
            age      :<input type="text" name="age" value="<?php echo $result['age'] ?>"></br>
            Address  :<input type="text" name="address" value="<?php echo $result['address'] ?>"></br>

            <input type="submit" value="update" name="submit">
        </form>
    </body>

</html>

3 Answers 3

3

You are only passing $id in

$this->usermodel->entry_update1($get['id']);

and in function u did

public function entry_update1($id) {
    $this->db->where('id', $id);
    $this->db->update('user', $data);
}

so you have to pass $data also in you function call

$this->usermodel->entry_update1($get['id'], $data);

public function entry_update1($id, $data) {
    $this->db->where('id', $id);
    $this->db->update('user', $data);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanx Rana but showing error... Array to string conversion
you $data array should be $data = array('title' => $this->input->post('title'),'name' => $this->input->post('name'),'date' => $this->input->post('date')); //replace your db fields
1

Just by having a quick look, I can see you're not passing anything to the $data in your entry_update1 function;

public function entry_update1($id) {

    $this->db->where('id', $id);
    $this->db->update('user', $data);

}

You're trying to update 'user' with $data, but you haven't set $data.

1 Comment

You're only passing an ID to this function ($id), but this function is looking for another variable, called $data used within the update()
0

You have to run update query this way in CodeIgniter.

        public function entry_update1($id,$data) {

            $this->db->set($data);
            $this->db->where('id',$id);
            $update = $this->db->update('user');
            if($update)
            {
                return true;
            }
            else
            {
               return false;
            }

         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.