Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating records codeigniter

I am trying to update a table where id of the row is entered and title is selected through a dropdown list, but I have two problems. Firstly Im not sure how to pass the data to the model, and the second problem is actually updating the table using active record.

controller class

class Update extends CI_Controller {

public function __construct()
{
        parent::__construct();
        //$this->load->model('addmodel');
        //$this->load->helper('form');
}

public function index()
{
    $this->load->view('updview');

}

public function updtitle() 
{   

    $data = array(
    'id' => $this->input->post('id'),
    'title' => $this->input->post('title') );
    //$data1['data'] = $data;

    $data1['data'] = $this->updmodel->upddata($data);
    $this->load->view('updview', $data1);
}
}

?>

model class

class Updmodel extends CI_Model {
// model constructor function
function __construct() {
    parent::__construct(); // call parent constructor
    $this->load->database();
}

public function upddata($data) {

    $this->db->where('emp_no', $data['id']);
    $this->db->update('title', $data['title']);

    return;
}
}

?>

view

Update employee title

<form action="http://localhost/ecwm604/index.php/update/updtitle" method="POST">
    employee id: 
        <input type=text name="id"><br />
    change title to: 
        <select name="title">
            <option value="Assistant Engineer">Assistant Engineer</option>
            <option value="Engineer">Engineer</option>
            <option value="Senior Engineer">Senior Engineer</option>
            <option value="Senior Staff">Senior Staff</option>
            <option value="Staff">Staff</option> 
        </select><br />
    <input type="submit" value="submit"/>
    <br />
<?php
    print_r($data);
    //echo $data['title'];
?>

</body>
</html>
like image 635
ethio Avatar asked Dec 15 '12 18:12

ethio


People also ask

How can I update my ID in CodeIgniter?

Just do like this: $this->db->where('id_colum', $student_id); $this->db->update('table_name', $data); $updated_status = $this->db->affected_rows(); if($updated_status): return $student_id; else: return false; endif; If it is updated, then return back that $student_id. :) Thanks for your Answer Mr.

What does this -> db -> update return?

You can use $this->db->affected_rows() in Codeigniter this returns a numeric value when doing "write" type queries (insert, update, etc.). In MySQL DELETE FROM TABLE returns 0 affected rows. The database class has a small hack that allows it to return the correct number of affected rows.

How do you check data is updated or not in CodeIgniter?

Mirov When we are working with codeigniter, the data only updated when there is some change in the input field's value and then the $this->db->affected_rows() will return value greater then 0.


1 Answers

In your Controller

public function updtitle() 
{   
    $data = array(
        'table_name' => 'your_table_name_to_update', // pass the real table name
        'id' => $this->input->post('id'),
        'title' => $this->input->post('title')
    );

    $this->load->model('Updmodel'); // load the model first
    if($this->Updmodel->upddata($data)) // call the method from the model
    {
        // update successful
    }
    else
    {
        // update not successful
    }

}

In Your Model

public function upddata($data) {
    extract($data);
    $this->db->where('emp_no', $id);
    $this->db->update($table_name, array('title' => $title));
    return true;
}

The active record query is similar to

"update $table_name set title='$title' where emp_no=$id"
like image 118
The Alpha Avatar answered Oct 21 '22 18:10

The Alpha