更新记录codeigniter [英] Updating records codeigniter

查看:71
本文介绍了更新记录codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更新一个表,其中输入了行的ID,并通过下拉列表选择了标题,但我有两个问题。
首先我不知道如何将数据传递给模型,第二个问题是使用活动记录更新表。

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.

控制器类

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);
}
}

?>

模型类

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;
}
}

?>

查看





更新员工职位

<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>


推荐答案

在您的控制器



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;
}

活动记录查询类似于

"update $table_name set title='$title' where emp_no=$id"

这篇关于更新记录codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆