Codeigniter: $this->session->userdata('user_id') funktioniert nicht richtig
P粉738346380
P粉738346380 2024-03-30 12:19:52
0
1
412

Ich versuche also, die Verwendung von Codeigniter 3 zu erlernen und versuche, die ID aus der Benutzertabelle abzurufen. Der Name der ID-Spalte in der Benutzertabelle ist user_id,我有另一个名为lowongan的表,它有id_user从用户表中获取 id(user_id)。因此,当我尝试输入并提交要保存到数据库中的输入时,id_user leer, aber ich habe ihn so eingestellt

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

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

Aber es ist immer noch leer. Wie kann ich das beheben?

Modell:

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

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

P粉738346380
P粉738346380

Antworte allen(1)
P粉803527801

您已经创建会话了吗?如果没有,您可以这样创建:

$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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage