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

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

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

Laravel update table

I'm trying to do simple function edit student table field, But its updating only first field: first_name
My controller.
public function editStudent(Request $request)
{
$studId = $request->get('studId');
$studFirstName = $request->get('firstName');
$studLastName = $request->get('lastName');
$studGender = $request->get('gender');
$studBirthday = $request->get('birthday');
$studSchoolId = $request->get('schoolId');
$update = Student::where('id', $studId)->update(['first_name' => $studFirstName],
['last_name' => $studLastName], ['gender' => $studGender], ['birthday', $studBirthday], ['school_id' => $studSchoolId]);
return response()->json([
"update" => $update,
]);
}
Update takes a single array, you're passing in multiple arrays
$update = Student::where('id', $studId)
->update([
'first_name' => $studFirstName,
'last_name' => $studLastName,
'gender' => $studGender,
'birthday'=> $studBirthday,
'school_id' => $studSchoolId
]);
Its better to get the item from DB then update that on a second query:
$student = Student::find($studId);
$student->update([
'first_name' => $request->firstName,
'last_name' => $request->lastName,
'gender' => $request->gender,
'birthday'=> $request->birthday,
'school_id' => $request->schoolId
]);

codeigniter : inserting data/record into 2 tables with the same id

I have two tables. the first table is tb_mhs and the second user.
how do you make the id_user in the tb_mhs table the same as the id_user in the user table?
thank you very much for your attention
tb_mhs
user
Controller Code :-
$email = $this->input->post('email');
$nama = $this->input->post('nama');
$password = SHA1($this->input->post('password'));
$data1 = array(
'email'=>$email,
'password'=>$password,
'nama'=>$nama,
'level'=>2,
'status'=>0
);
$i=$this->input;
$npm = $this->register_m->create('user',$data1);
$data = array(
'email' => $i->post('email'),
'password' => SHA1 ($i->post('password')),
'npm' => $i->post('npm'),
'nama' => $i->post('nama'),
'j_kelamin' => $i->post('j_kelamin'),
'kelas_id' => $i->post('kelas_id'),
'angkatan_id' => $i->post('angkatan_id'),
'internal_id' => $i->post('internal_id'),
'eksternal_id' => $i->post('eksternal_id'),
'latitude' => -6.873776,
'longitude' => 107.575639,
'berkas' => $i->post('berkas'),
);
// $insert = $this->register_m->create('user',$data1);
$insert1 = $this->register_m->create('tb_mhs',$data);
$this->session->set_flashdata('sukses', 'Data Registrasi Berhasil di Tambahkan');
redirect(base_url('register/viewdataregistrasi'), 'refresh');
}
}
Model
public function create($table, $data)
{
$query = $this->db->insert($table, $data);
return $this->db->insert_id();
}
CI has Query Helper function call $this->db->insert_id(), it returns the insert ID number when performing database inserts.
After the line
$npm = $this->register_m->create('user',$data1);
Add this code
$newUserID = $this->db->insert_id();
Then update your $data array like this
$data = array[
'id_user' => $newUserID,
'email' => $i->post('email'),
'password' => SHA1 ($i->post('password')),
'npm' => $i->post('npm'),
'nama' => $i->post('nama'),
'j_kelamin' => $i->post('j_kelamin'),
'kelas_id' => $i->post('kelas_id'),
'angkatan_id' => $i->post('angkatan_id'),
'internal_id' => $i->post('internal_id'),
'eksternal_id' => $i->post('eksternal_id'),
'latitude' => -6.873776,
'longitude' => 107.575639,
'berkas' => $i->post('berkas'),
];
Rest of code is good.
For more information Read.

How to use a request field to save new a variable with that value?

So, I have a function that creates me alerts based on changes made on a profile.And I need to have customizable alerts for every field, like name, city, email etc.Now, I need to make this working and I also need to show me an alert like ... User x changed his name to y, or User z changed his email to w.
Here is my controller
public function generalinfo(Request $request)
{
if ( $user = Sentinel::getUser() ) {
if ( $user->inRole('individuals') ) {
$data = $this->data;
$id = $user->id;
$user = User::findOrfail($id);
$generalinfo = User::firstOrNew(array('email' => $user->email ));
$dob = date("Y-m-d", strtotime($request->get('dob')));
$generalinfo->name = $request['name'];
$generalinfo->city = $request['city'];
$generalinfo->address = $request['address'];
$generalinfo->email = $request['email'];
$generalinfo->website1 = $request['website1'];
$generalinfo->website2 = $request['website2'];
$generalinfo->website3 = $request['website3'];
$generalinfo->save();
$feed = \App\Feed::create([
'user_id' => $id,
'feed_url' => $request['rss'],
'active' => 1
]);
$generalinfo = User::find($id);
$generalinfo->name = 'NewName';
// other $user changes
foreach ($generalinfo->getDirty() as $key => $attribute) {
$log = \App\Log::create([
'user_id' => $id,
'log' => " {$key} changed to {$user->user}.",
'date' => time(),
'type' => 'profile_updates',
'formatted_date' => date_format(date_create(date('Y-m-d')),"F dS, Y"),
'action_user_id' => $id,
'action_name' => $user->name,
'action_username' => $user->username,
'profile_picture' => $user->profile_picture,
]);
}
$generalinfo->save();
}
}
}
This is my problem : $generalinfo->name = 'NewName'; .I need to get the new value from database from $user->name. The problem is, I tried to use this : $generalinfo->name = $request['name']; but with no effect.

How to save id no. as a image name in yii?

I am a begineer in yii. I am making a basic structure - an employee. I am uploading their images and saving image name in database and also showing it on my view page. But I want to change the image name to employee id.
For Ex:
desert.jpg changes to 01.jpg or any other employee id.
Please help me.
CONTROLLER:
public function actioncreate(){
$dept_list = department_mod::model()->dept_list();
$data['dept_list'] = CHtml::listData($dept_list,'value', 'text');
$team_list = team_mod::model()->team_list();
$data['team_list'] = CHtml::listData($team_list,'value', 'text');
$des_list = employee_mod::model()->des_list();
$data['des_list'] = CHtml::listData($des_list,'value', 'text');
$report_to_list = report_to_mod::model()->report_to_list();
$data['report_to_list'] = CHtml::listData($report_to_list,'value', 'text');
$model_upload = new upload_mod;
$data['model_upload'] = $model_upload;
$this->render('create',$data);
}
public function actionsave(){
$q = Employee_mod::model()->data_save($_POST);
$data = $_POST;
$exp = 'profile';
$emp_upload_path = 'assests/images/';
$p = $emp_upload_path;
echo $p;
if (!is_dir($p)) {
mkdir($p,0777);
}
$path = $p.$exp.'/';
if (!is_dir($path)) {
mkdir($path,0777);
}
$model_upload = new upload_mod;
$file_name = "";
$model_upload->upload_file = CUploadedFile::getInstance($model_upload,'userfile');
if($model_upload->validate()) {
$model_upload->upload_file->saveAs($path . $model_upload->upload_file);
$file_name = $model_upload->upload_file;
}else{
var_dump($model_upload->getErrors());
}
$data['file_name'] = $file_name;
$q = Employee_mod::model()->imagesave($q , $file_name);
$this->redirect(array('employee/list'));
MODEL:
function data_save($data)
{
$input = array(
'first_name' => $data['first_name'],
'middle_name' => $data['middle_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'cnic' => $data['cnic'],
'dob' => $data['dob'],
'department' => $data['dept_id'],
'location' => $data['location'],
'created_by' => 'admin',
'team' => $data['id'],
'des' => $data['des_id'],
'report_to' => $data['id'],
'joining_date' => $data['joining_date'],
);
$q = Yii::app()->db->createCommand();
$q->insert('emp',$input);
$insert_id = Yii::app()->db->getLastInsertID();
return $insert_id;
}
function imagesave($empno , $file_name)
{
$file = array(
'image' => $file_name
);
$q = Yii::app()->db->createCommand();
$q->update('emp', $file, 'empno=' . $empno);
}
function imageeditsave($empno , $file_name)
{
$file = array(
'image' => $file_name
);
$q = Yii::app()->db->createCommand();
$q->update('emp', $file, 'empno=' . $empno);
}
function data_editsave($data,$emp_no)
{
$input = array(
'first_name' => $data['first_name'],
'middle_name' => $data['middle_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'cnic' => $data['cnic'],
'dob' => $data['dob'],
'department' => $data['dept_id'],
'location' => $data['location'],
'created_by' => $data['created_by'],
'team' => $data['id'],
'des' => $data['des_id'],
'report_to' => $data['id'],
'joining_date' => $data['joining_date'],
);
$q = Yii::app()->db->createCommand();
$q-> update('emp',$input,'empno =' . $emp_no);
$insert_id = Yii::app()->db->getLastInsertID();
return $insert_id;
}
The problem is here:
$q = Yii::app()->db->createCommand();
$q->update('emp', $file, 'empno=' . $empno);
update is an ActiveRecord method and you are using it with createCommand! You need to change that code to this:
Employee_mod::model()->updateByPk($empno, array('image' => $file_name));

get primary key to put as foreign key in other table

I have this code, what I want to do is to get the primary key lastly inserted into the user table and then put that into the foreign key of the library table. I don't know how to do it with $request or what to do here?
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$data1 = array(
'user_id' => '5',// this is where I have to put the primary key from last table
'library_name' => $request['lib_name']
);
$this->model->insert('user', $data);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
Use mysql_insert_id()
-- Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.
You can do like this
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$this->model->insert('user', $data);
$user_id = $this->model->select('user', $data); //select user_id from db
$data1 = array(
'user_id' => $user_id ,
'library_name' => $request['lib_name']
);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
you can do it like this
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$data1 = array(
'library_name' => $request['lib_name']
);
$this->model->insert('user', $data);
$user_id = mysql_insert_id();
$data1['userid'] = $user_id;
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
If you are using any frameworks. then they have the helper functions to get last insert id, you should use them
Thanks guys, it worked all i needed to do was just to change the logic, I was using it stupidly and didn't see where to place the insert query so Its working now all i had to do was just to change the place of the insert query as given
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$this->model->insert('user', $data);
$data1 = array(
'user_id' => mysql_insert_id(),// now it works as insert has been done above ....
'library_name' => $request['lib_name']
);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');

Categories