retrive primary_key of just inserted data in codeigniter - php

I just added the data in mysql with CI query builder. Now I need to retrive the primary key(Track Code) of those data(row). I used insert and select query simultanously but it didn't worked.
Model:
public function complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip)
{
$data = array(
'cName' => $cName,
'vName' => $vName,
'Email' => $Email,
'Contact' => $Contact,
'Date' => $date,
'Complain' => $Complain,
'ip' => $ip
);
$sql= $this->db->set($data)->get_compiled_insert('tbl_complain');
$q=$this->db->query($sql);
return $q;
}
Controller :
public function index()
{
if (isset($_POST['btnRegister']))
{
$cName=$this->input->post('cName');
$vName=$this->input->post('vName');
$Email=$this->input->post('email');
$Contact=$this->input->post('phone');
$date=$this->input->post('Date');
$Complain=$this->input->post('complain');
$ip=file_get_contents("http://ipecho.net/plain");
$this->HamroSamajModel->complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip);
$this->session->set_flashdata("message","Your complain has been registered sucessfully");
}
$this->load->view('Complain/index');
}

If you need last Insert ID just try this
public function complainReg($cName,$vName,$Email,$Contact,$date,$Complain,$ip)
{
$data = array(
'cName' => $cName,
'vName' => $vName,
'Email' => $Email,
'Contact' => $Contact,
'Date' => $date,
'Complain' => $Complain,
'ip' => $ip
);
$this->db->insert('tbl_complain', $data);
$lastID = $this->db->insert_id();
return $lastID;
}

Related

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

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

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.

Laravel insert into new form, with existing users

I've created a new form for existing users.
And i'm trying to insert the new values into the database, but the values wont save.
public function editUser(Request $request, $id, $check)
{
$curID = $id;
$userData = $request->input();
$curUser = User::Where('id', $curID)->first();
$address = Address::where('id', $userData['address_id']);
------Not working----------------
if(empty($curUser->$address)){
$curUser->insert($address)([
'address_id' => $address->id,
'region_id' => $userData['region'],
'country_id' => $userData['country'],
'city_id' => $userData['city'],
'street_name' => $userData['street_name'],
'house_number' => $userData['house_number'],
'postcode' => $userData['postcode']
]);
-------Not working----------------
} else{
$address->update([
'region_id' => $userData['region'],
'country_id' => $userData['country'],
'city_id' => $userData['city'],
'street_name' => $userData['street_name'],
'house_number' => $userData['house_number'],
'postcode' => $userData['postcode'],
]);
return redirect('client')->with($this->messageBag, "User edited!");
}
public function editUser(Request $request, $id, $check)
{
$user = User::where('id',$id)->first();
$address = Address::where('id',$request->input('address_id'))->first();
if(empty($address)){
$address=new Address();
}
$address=$this->saveData($address,$request()->all());
$user->update(['address_id'=>$address->id]);
return redirect('client')->with($this->messageBag, "User edited!");
}
protected function saveData(Address $address,array $userData){
$address->region_id= $userData['region'];
$address->country_id= $userData['country'];
$address->city_id= $userData['city'];
$address->street_name= $userData['street_name'];
$address->house_number= $userData['house_number'];
$address->postcode= $userData['postcode'];
$address->save();
return $address;
}

last inserted id in Php codeIgniter

In my model I have return below function for get record id
function getLastInserted()
{
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration");
return $query->result();
}
Now I want to pass that ID to mycontroller for record insertion
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
Now I want to access model function in controller and pass record id in data function How I do ??
set return in model and in controller write
$insert_id=$this->db->insert_id();
In Controller load your model first using
$this->load->model('model_name');
Then call to model's getLastInserted() function.
$ID = $this->model_name->getLastInserted();//you will get id here
In model return $query->row()->userid; insead return of $query->result().
Then modify the controller:
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $this->model_name->getLastInserted(),
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
hi i worked out the solution on last inserted id dear here the code:
controller:
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
model:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
model:
public function newregistration($sid)
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $sid,
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}

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