在会话中获取更新记录

时间:2016-04-19 07:27:04

标签: php codeigniter session

我正在尝试使用会话存储在任何用户配置文件中更新的最新记录,记录在数据库中更新但无法在会话中获取最新记录值,会话显示旧值。

这是我的控制器类的代码

// Update Student Information
    public function model_studentupdate() {
        $studentid          = $this->db->escape($this->session->userdata('student_id'));
        $studentname        = $this->db->escape($this->input->post("name"));
        $studentcontact     = $this->db->escape($this->input->post("contact"));
        $studentcity        = $this->db->escape($this->input->post("city"));
        $studentcountry     = $this->db->escape($this->input->post("country"));

        // Update Record in User Table
        $this->db->query("UPDATE `ot4u_users` SET
        `user_name`=$studentname,
        `user_phone`=$studentcontact,
        `user_country`=$studentcountry,
        `user_city`=$studentcity
        WHERE `user_id` = $studentid");

        // Get All Records of Student
        $query  = $this->db->query("SELECT * FROM `ot4u_users` WHERE ot4u_users.user_id = $studentid");
        $query  = $query ->result();

            // Store Latest Data of Tutor in Session
            $tutorSessionData = array(
            'student_id'        => $query[0]->user_id,
            'student_name'      => $query[0]->user_name,
            'student_phone'     => $query[0]->user_phone,
            'student_city'      => $query[0]->user_city,
            'student_country'   => $query[0]->user_country,
            'is_logged_in'      => true
            );
            $this->session->set_userdata($studentSessionData);
            return true;
    }

我的代码中缺少什么?请帮忙

2 个答案:

答案 0 :(得分:2)

你有两个不同的变量:

$tutorSessionData = array(
        //etc
        );
        $this->session->set_userdata($studentSessionData);

应该是

$studentSessionData = array(
        //etc
    );
 $this->session->set_userdata($studentSessionData);

答案 1 :(得分:0)

你在最后一行传递了错误的变量

/ Store Latest Data of Tutor in Session
            $tutorSessionData = array(
            'student_id'        => $query[0]->user_id,
            'student_name'      => $query[0]->user_name,
            'student_phone'     => $query[0]->user_phone,
            'student_city'      => $query[0]->user_city,
            'student_country'   => $query[0]->user_country,
            'is_logged_in'      => true
            );
            $this->session->set_userdata($tutorSessionData);
            return true;
    }