CodeIgniter单个更新查询使用现有列数据更新一列?

时间:2013-09-09 05:29:25

标签: php mysql codeigniter activerecord

在我的数据库中,我有一个datetime的Last和Current列。只是为了留意有人上次使用有效登录我正在建立的服务。

我想知道的是,是否可以使用该行中的另一列更新行上的一列,同时更新另一列

即:将Last设置为当前Current,然后将Current设置为当前的日期?

有道理吗?

9 个答案:

答案 0 :(得分:47)

试试这样..

$data=array('current_login'=>date('Y-m-d H:i:s'));
$this->db->set('last_login','current_login',false);
$this->db->where('id','some_id');
$this->db->update('login_table',$data);

上面代码生成的查询: -

UPDATE login_table SET last_login = current_login,current_login ='2018-01-18 15:24:13'WHERE id ='some_id'

答案 1 :(得分:3)

如果您只想升级表行的单个列,则可以使用如下:

$this->db->set('column_header', $value); //value that used to update column  
$this->db->where('column_id', $column_id_value); //which row want to upgrade  
$this->db->update('table_name');  //table name

答案 2 :(得分:2)

$this->db->set('usage', 'usage');  //Set the column name and which value to set..

$this->db->where('tag', 'java'); //set column_name and value in which row need to update

$this->db->update('tags'); //Set your table name

试试这个......

答案 3 :(得分:1)

$data = array( 
    'name'      => $_POST['name'] , 
    'groupname' => $_POST['groupname'], 
    'age'       => $_POST['age']
);

$this->db->where('id', $_POST['id']);

$this->db->update('tbl_user', $data);

答案 4 :(得分:1)

尝试以下代码:

$data = array(
    'name' = > $_POST['name'] ,
    'groupname'= > $_POST['groupname'],
    'age' = > $_POST['age']
);
$this->db->where('id', $_POST['id']);
$this->db->update('tabname', $data);

答案 5 :(得分:0)

我将此功能用于CI网站中的所有更新

function _updateRowWhere($table, $where = array(), $data = array()) {
    $this->db->where($where);
    $this->db->update($table, $data);
}

答案 6 :(得分:0)

        $id= $this->input->Post('edit_id');
        $date= $this->input->Post('date');
        $title=$this->input->Post('title');
        $content=$this->input->Post('content'); 

 $value=array('date'=>$date,'content'=>$content,'title'=>$title);
 $this->db->where('course_id',$id);
 if( $this->db->update('course',$value))
      {
        return true;
      }
      else
      {
        return false;
      }

答案 7 :(得分:0)

替代性的编写更新查询的方式如下...

$this->db->update('student',array('class'=>'xyz','name'=>'abc'),array('stud_id'=>20);
  

设置类xyz,名称abc,其中“学生”表中的stud_id = 20。.

答案 8 :(得分:0)

是的,这是可能的,我想提供一种替代Rajeev的答案的方法,该方法不会将php生成的日期时间格式的字符串传递给查询。

关于如何在UPDATE查询中声明要设置为SET的值的重要区别在于,不得将它们引用为文字字符串。

要防止CodeIgniter自动执行此“收藏夹”,请使用set()方法和第三个参数false

$userId = 444;
$this->db->set('Last', 'Current', false);
$this->db->set('Current', 'NOW()', false);
$this->db->where('Id', $userId);
// return $this->db->get_compiled_update('Login');  // uncomment to see the rendered query
$this->db->update('Login');
return $this->db->affected_rows();  // this is expected to return the integer: 1

生成的查询(取决于您的数据库适配器)将如下所示:

UPDATE `Login` SET Last = Current, Current = NOW() WHERE `Id` = 444

该查询有效的证明:https://www.db-fiddle.com/f/vcc6PfMcYhDD87wZE5gBtw/0

在这种情况下,LastCurrent是MySQL关键字,但它们不是保留关键字,因此不需要进行反引号包装。

>

如果您的精确查询需要使用正确的带引号的标识符(表/列名),那么总会有protectIdentifiers()