Home > Mobile >  Codeigniter : $this->session->userdata('user_id') not working
Codeigniter : $this->session->userdata('user_id') not working

Time:01-04

So i'm trying to learn using codeigniter 3, and i'm trying to get the id from users table, the name of the id column on users table is user_id, and i have another table called lowongan, it has id_user to get the id(user_id) from the users table. So when i tried to input and submit that input to be saved into the database, the id_user is null, but i already set it like this

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

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

But it's still null how can i fix this??

The model :

<?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);
    }
}

The Controller :

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');
    }

CodePudding user response:

do you already create the session? If not, you can create like this:

$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);

After that you can easily call the session like:

$this->session->name
  • Related