In my codeigniter database I have 2 tables called user and user_group. When I create new user I need it to be able to get the first id from user_group which is administrator. And the user_group_id for administrator is 1
It currently gets the second row which is user_group_id number '10'
Problem Can Not Get The First Row ID Of User Group
Here is my insert model
public function insert() {
$password = $this->input->post('password');
$username = $this->input->post('username');
$email = $this->input->post('email');
$data = array(
'username' => $username,
'password' => $this->hash($password),
'status' => "1",
'ip' => $this->input->ip_address(),
'user_group_id' => $this->db->insert_id(),
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->set($data);
$this->db->insert('user');
return $this->db->insert_id('user_id');
}
I was able to get the user id as per normal and I hard coded the user_group_id this is how I did it below on my Model.
public function user() {
$this->db->escape($username = $this->input->post('username'));
$this->db->escape($email = $this->input->post('email'));
$this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9));
$this->db->escape($password = sha1($salt . sha1($salt . sha1($this->input->post('password')))));
$data = array(
'username' => $username,
'password' => $password,
'status' => "1",
'salt' => $salt,
'ip' => $this->input->ip_address(),
'user_group_id' => "1",
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->set($data);
$this->db->insert('user');
return $this->db->insert_id();
}
Related
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
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.
public function register() {
if (isset($_POST['register'])) {
$u = $_POST['uname'];
$this->load->database();
$this->load->database();
$this->db->select('uname');
$this->db->from('login');
$this->db->where(array('uname' => $u));
$query1 = $this->db->get();
if (!$query1->num_rows() == 1) {
$data = array(
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'dob' => $_POST['dob'],
'gender' => $_POST['gender'],
'email' => $_POST['email']
);
$this->load->database();
$this->db->insert('user', $data);
$data1 = array(
//'User_idUser'=>$_POST[$query1],
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);
$this->db->insert('login', $data1);
} else {
$_SESSION["ex"] = "User All Ready Exists";
}
}
$this->load->view('register');
}
I want to get auto increment id of user table to save in login table as a foreign key to identify which user is currently logging. the code is running perfectly.
the user table is a parent table and
login table is a child table.
User following code to get ID of last insert operation.
$insert_id = $this->db->insert_id();
And use $inser_id to add it to any table you want.
So if I open the commented line of your code,
$data1 = array(
'User_idUser' => $insert_id, // Assuming that UseridUser is your columne to store the Autoincremented user id from previous insert query.
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);
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;
}
I want to be able to use codeigniter db escape because it hashes my passwords OK but in the password row it adds two quotes on either side Example 'demo1234'
My salt row does not show it which is OK just the password. On password how do I use my db escape but not having the quotes show up on password row in db.
public function addUserToDatabase() {
$this->load->helper('date');
$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)); // Works OK
// Salt does not work if don't use db escape.
$password = $this->db->escape(sha1($salt . sha1($salt . sha1($password)))); // 'demo1234' shows quotes either side off password
$data = array(
'user_id' => "1",
'user_group_id' => "1",
'username' => $username,
'email' => $email,
'salt' => $salt,
'password' => $password,
'ip' => $this->input->ip_address(),
'status' => "1",
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->insert('user', $data);
}
I worked it out I had to do it like this below, all done.
public function addUserToDatabase() {
$this->load->helper('date');
$username = $this->input->post('username');
$email = $this->input->post('email');
$this->db->escape($password = sha1($salt . sha1($salt . sha1($this->input->post('password')))));
$this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9));
$data = array(
'user_id' => "1",
'user_group_id' => "1",
'username' => $username,
'email' => $email,
'salt' => $salt,
'password' => $password,
'ip' => $this->input->ip_address(),
'status' => "1",
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->insert('user', $data);
}