Updating records codeigniter

2019-01-26 20:27发布

问题:

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>

回答1:

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"


回答2:

In your_controller write this...

public function update_title() 
{   
    $data = array
      (
        'table_id' => $this->input->post('table_id'),
        'table_title' => $this->input->post('table_title')
      );

    $this->load->model('your_model'); // First load the model
    if($this->your_model->update_title($data)) // call the method from the controller
    {
        // update successful...
    }
    else
    {
        // update not successful...
    }

}

While in your_model...

public function update_title($data)
{
   $this->db->set('table_title',$data['title'])
         ->where('table_id',$data['table_id'])
        ->update('your_table');
}

This will works fine...



回答3:

In codeigniter doc if you update specific field just do this

$data = array(
    'yourfieldname' => value,
    'name' => $name,
    'date' => $date
);

$this->db->where('yourfieldname', yourfieldvalue);
$this->db->update('yourtablename', $data);


回答4:

How to update in codeignitor?

whenever you want to update same status with multiple rows you use where_in insteam of where or if you want to change only single record can use where.

below is my code

$conditionArray = array(1, 3, 4, 6);
$this->db->where_in("ip_id", $conditionArray);
$this->db->update($this->table, array("status" => 'active'));

its working perfect.