php Codeigniter 从控制器内的查询更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18882587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:23:49  来源:igfitidea点击:

Codeigniter update from query within controller

phpmysqlcodeignitervalidationcontroller

提问by user2505513

Hope I can explain this properly...

希望我能正确解释这个......

I am using form validation with Codeigniter. If my form returns a string id back to the controller, how can I use this to perform a database query and use the result array to perform an update...

我正在使用 Codeigniter 进行表单验证。如果我的表单将字符串 id 返回给控制器,我如何使用它来执行数据库查询并使用结果数组来执行更新...

Does that make sense?

那有意义吗?

function form($id) {
    if ($this->form_validation->run('') == false) {
        //...
    }
    else {
        // Query database where strId = $this->input->post('strId')
        // Put results into an array called $data
        // Update database where id = $this->input->post('id')

        $this->db->where('id', $this->input->post('id'));
        $this->db->update('database', $data);
    }
}

回答by Aaron Martins

Controller:

控制器:

    //... (if valid) ...
    $this->load->model('myform'); //loads myform_model.php
    $this->myform->update(); //call the update method

Model (myform_model.php):

模型(myform_model.php):

    function update(){
        $this->load->database();
        $this->db->where('id', $this->input->post('id'));
        $this->db->update('tableName', $data);
    }

Note: Your id must already exist in the database table since you're using "update". To insert data, use $this->db->insert();

注意:您的 id 必须已经存在于数据库表中,因为您使用的是“更新”。要插入数据,使用 $this->db->insert();

回答by k...

nice code excellent :

很好的代码很好:

function update($id, $data) {

函数更新($id,$data){

    $this->db->where('id', $this->input->post('$id'));
    $this->db->update('tableName', $data);
}

or lik this:

或者像这样:

function update($id, $data) {

函数更新($id,$data){

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