首頁  >  問答  >  主體

Codeigniter:$this->session->userdata('user_id') 無法正常運作的問題

所以我正在嘗試使用codeigniter 3進行學習,並且我正在嘗試從用戶表中獲取id,用戶表上的id列的名稱是user_id,我有另一個名為 lowongan的表,它有id_user從使用者表中取得id(user_id)。因此,當我嘗試輸入並提交要儲存到資料庫中的輸入時,id_user 為空,但我已經這樣設定了

$id_user            = $this->session->userdata('user_id');

$data = array(
'id_user' => $id_user
);

但它仍然為空,我該如何解決這個問題?

型號:

<?php

class M_lowongan extends CI_Model{
    public function show_lowongan(){
        return $this->db->get('lowongan');
    }

    public function input_data($data, $table){
        $this->db->insert($table, $data);
    }
}

控制器:

public function store_lowongan(){
        $id_user            = $this->session->userdata('user_id');
        $title              = $this->input->post('title');
        $lokasi             = $this->input->post('lokasi');
        $level_pekerja      = $this->input->post('level_pekerja');
        $pengalaman_kerja   = $this->input->post('pengalaman_kerja');
        $pendidikan         = $this->input->post('pendidikan');
        $alamat             = $this->input->post('alamat');
        $no_wa              = $this->input->post('no_wa');
        $no_telp            = $this->input->post('no_telp');
        $min_gaji           = $this->input->post('min_gaji');
        $max_gaji           = $this->input->post('max_gaji');
        $job_desc           = $this->input->post('job_desc');

        $data = array(
            'id_user'           => $id_user,
            'title'             => $title,
            'lokasi'            => $lokasi,
            'level_pekerja'     => $level_pekerja,
            'pengalaman_kerja'  => $pengalaman_kerja,
            'pendidikan'        => $pendidikan,
            'alamat'            => $alamat,
            'no_wa'             => $no_wa,
            'no_telp'           => $no_telp,
            'min_gaji'          => $min_gaji,
            'max_gaji'          => $max_gaji,
            'job_desc'          => $job_desc
        );

        $this->m_lowongan->input_data($data, 'lowongan');
        redirect ('dashboard');
    }

P粉738346380P粉738346380226 天前388

全部回覆(1)我來回復

  • P粉803527801

    P粉8035278012024-03-31 10:32:36

    您已經建立會話了嗎?如果沒有,您可以這樣建立:

    $row = $this->Auth_model->get_by_username($this->input->post('username'));
    
    $session = array(
          'id_users'    => $row->id_users,
          'name'        => $row->name,
          'username'    => $row->username,
          'email'       => $row->email,
          'usertype'    => $row->usertype,
          'photo'       => $row->photo,
          'photo_thumb' => $row->photo_thumb,
        );
    
    $this->session->set_userdata($session);

    之後您可以輕鬆地呼叫會話,如下所示:

    $this->session->name

    回覆
    0
  • 取消回覆