10

我正在尝试更新输入行的 id 并通过下拉列表选择标题的表,但我有两个问题。首先我不确定如何将数据传递给模型,第二个问题实际上是使用活动记录更新表。

控制器类

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

5 回答 5

31

在你的控制器中

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
    }

}

在您的模型中

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"
于 2012-12-15T19:36:46.397 回答
2

在 your_controller 写这个...

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

}

在你的模型中...

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

这将工作正常...

于 2014-12-15T12:45:46.783 回答
2

如何在codeignitor中更新?

每当您想用多行更新相同的状态时,您可以使用 where_in insteam of where 或者如果您只想更改单个记录可以使用 where。

下面是我的代码

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

它的工作完美。

于 2018-08-16T04:43:19.387 回答
1

在 codeigniter 文档中,如果您更新特定字段,只需执行此操作

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

$this->db->where('yourfieldname', yourfieldvalue);
$this->db->update('yourtablename', $data);
于 2018-06-11T03:51:16.137 回答
-1
function getBankMaster()
{
    
    $this->db->select('kr.id as kr,bk.id as bk_id ');    
    $this->db->from("ut_axis_klot as kr");       
    $this->db->join("ut_axis_bankmaster as bk","right(rtrim(kr.funding_ac),7) = right(rtrim(bk.funding_ac),7)","left");       
    $this->db->having('bk_id is null');     

    $query = $this->db->get();  

    // echo $this->db->last_query();die;

    if($query->num_rows() > 0)
    {
        $data = $query->result();

        foreach($data as $val)
        {
            $id = $val->kr;

            $this->db->where('id', $id);
            $this->db->update($this->db_sdip, array('amc_remark' => 'Wrong Account Number'));                
        }

       
    }
    else
    {
        return NULL;
    }
}
于 2021-12-10T12:12:41.510 回答