php 代码点火器:$this->会话->用户数据('user_id')不工作

oknwwptz  于 2023-01-04  发布在  PHP
关注(0)|答案(1)|浏览(79)

我正在尝试学习如何使用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');
    }
zqry0prt

zqry0prt1#

是否已创建会话?如果尚未创建,可以按如下方式创建:

$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

相关问题