IF之后的PHP代码未执行


PHP code after IF not executed

我有这个函数

public function logout() {
    if ($this -> user_model -> is_logged() === TRUE) {
        $user_id = $this -> user_model -> get_id();
        if (get_cookie('cookie_name') != FALSE) {
            delete_cookie('cookie_name');
            $new_cookie = md5(sha1(rand() . time() . $user_id));
            $this -> db -> set('cookie', $new_cookie);
            $this -> db -> update('users') -> where('id = ' . "'" . $user_id . "'");
        }
        $this -> session -> unset_userdata('user_id');
        echo "logged out? <a href=" . base_url() . ">click here</a>";
    } else {
        echo "no";
    }
}

if (get_cookie('cookie_name') != FALSE) {

为true,如果括号内的代码被执行,则其他两行代码:

   $this -> session -> unset_userdata('user_id');
   echo "logged out? <a href=" . base_url() . ">click here</a>";

当if代码没有被执行时,这些行会被正确执行。

if内部的代码被正确执行,因为cookie被删除,数据库被更新。

我使用的是Codeigniter 2.1.0,但我不认为这个问题与此有关。

我的php版本是Ubuntu 11.04 上localhost下的5.3

正如我上面所评论的,您的更新语句需要分离出来。你需要更改这一行:

$this -> db -> update('users') -> where('id = ' . "'" . $user_id . "'"); 

到此:

$this->db->where('id = ' . "'" . $user_id . "'"); 
$this->db->update('users');

请注意,where子句在update语句之后被"清空",如果您计划在未来进行多个更新,则必须重复where子句。