Undefined index: email in codeigniter - codeigniter

im trying to log in but i keep encountering the error below. attached is my controller and model
controller
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Model_students->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->getImg();
redirect('Students/homepage');
}
else{
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
model
public function login($email,$password)
{
// $query = $this->db->get_where('users', array('email'=>$email, 'password'=>$password));
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
wheni i run this i get an error:
Message: Undefined index: email
Filename: controllers/Students.php
Line Number: 145

please don't use $_POST parameter without validation. use input library
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$post = $this->input->post(NULL, TRUE);
$email = isset($post['email']) ? $post['email'] : '';
$password = isset($post['password']) ? $post['password'] : '';
$data = $this->Model_students->login($email, $password);
if($data && !empty($data)){
$id = $data[0]->id;
$first_name = $data[0]->firstname;
$last_name = $data[0]->lastname;
$this->session->set_userdata('user_id', $id);
$this->session->set_userdata('lname', $last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname', $first_name);
$this->getImg();
redirect('Students/homepage');
}else{
$this->session->set_flashdata('error', 'Invalid login. User not found');
}
}

Related

homepage not loading. after signing in it redirects to signin instead of homepage

the login function. id like to know where im going wrong in this
controller
public function login(){
$this->load->library('session');
$this->load->model('Model_students');
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Model_students->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->getImg();
redirect('Students/homepage');
}
else{
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
public function getImg()
{
$userid=$this->session->userdata('user_id');
$res=$this->Model_students->getimge($userid);
$img=$res[0]->userfile;
$img1=json_decode($img);
$this->session->set_userdata('img',$img1);
}
model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
}
public function getimge($userid)
{
$query = $this->db->get_where('user_images', array('user'=>$userid));
if($query->num_rows() == 1 )
{
return $query->result();
}
}
after i sign in using credentials the page redirects to the signin instead of loading the view of the homepage.where could the problem be because it when i run this doesn't work as its supposed to

Cannot Update On Laravel 5.5

I get problem.
This is my controller
public function finish(Request $request)
{
$result = $request->input('data');
//$data = json_decode($result, true);
return $this->InvoiceBayar($result);
}
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data['transaction_status'];
$type = $data['payment_type'];
$order_id = $data['order_id'];
$fraud = $data['fraud_status'];
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}
This is my Route
Route::POST('/notification', 'SnapController#finish');
When Payment gateway, send a parameter to me, I cannot update DB.
But when I use POSTMAN. I success update DB
You need to use $request->all() as it will contain all payment gateway data.
public function finish(Request $request)
{
$result = $request->all();
return $this->InvoiceBayar($result);
}
Alternately you can do this
$update = Fee::where('invoice',$order_id)->first();
$update->status = 'Paid';
$update->save();
You should try this:
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data->transaction_status;
$type = $data->payment_type;
$order_id = $data->order_id;
$fraud = $data->fraud_status;
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}

Codeigniter: Display an image stored in DB

How do I get an image stored in a field of my database, and put it into a variable for using it in an email message like a signature?
Here is my code:
//Model user_model
function getImage(){
$this->db->select("image");
$this->db->where("$id", user_id);
$query = $this->db->get("users");
if($query->num_rows()>0){
return result();
}else{
return false;
}
}
//Controller user_controller
function image(){
this->load->model("user_model");
$id = $this->session->userdata('id');
$image = $this->user_model->getImage($id);
echo $image;
}
Here is a example
Model user_model
// Pass the id function
function getImage($id){
$this->db->where($id, 'user_id');
$query = $this->db->get("users");
if($query->num_rows()>0){
return $query->row_array();
}else{
return false;
}
}
Controller user_controller
function image(){
$this->load->model("user_model");
$somedata = $this->user_model->getImage($this->session->userdata('id'));
$data['image'] = $somedata['image'];
$this->load->view('someview', $data);
}
On view
<?php echo $image;?>

Select query in codeigniter not working with mysqli

Mysql query executed in mysql but not in mysqli and mysql is deprecated so what syntax I have to use for mysqli in codeigniter:
$sql = "SELECT admin_email
FROM `tbl_admin`
WHERE `admin_email` = '" . $username . "' and `admin_password` = '" . $password . "'";
$query = $this->db->query($sql);
You can use Codeigniter Query Bulider
$this->db->select('admin_email');
$this->db->from('tbl_admin');
$this->db->where('admin_email', $username);
$this->db->where('admin_password', $password);
$query = $this->db->get();
I hope it works...
$this->db->select('*');
$this->db->from('tbl_admin');
$this->db->where('admin_email', $username);
$this->db->where('admin_password', $password);
$query = $this->db->get();
$result = $query->row();
return $result;
Try ...
$this->db->select('admin_email');
$this->db->where(
array(
'admin_email' => $username,
'admin_password' => $password
));
$query = $this->db->get('tbl_admin');
$result = $query->row();
Hopefully it works in your end...
$this->db->where('admin_email',$email);
$this->db->where('admin_password',$password);
$qry = $this->db->get('tbl_admin');
if($qry->num_rows() > 0){
return true;
}else{
return false;
}
It will work fine only if you set your Query in the Model then load that model in your controller. Directly setting the query from Controller seems to have this issue.
Example:
From the Model: Example 'Users.php'
class Users extends CI_Model {
public function __construct() {
parent::__construct();
}
function check_username($username) {
$query = $this->db->query("select * from `user_login` where `username`='$username'")
$query_result = $query->result_array;
if (!empty($query_result)) {
return $query_result;
}
return[];
}
}
From the Controller: Example 'Userauth.php'
class Userauth extends CI_Controller {
public function process_forgot(){
$CI =& get_instance();
$CI->load->model('Users'); //Model loaded here
$username = $this->input->post('username');
$check = $CI->Users->check_username($username);
echo '<pre>';
print_r($check);
echo '</pre>';
}
}
Try it.

Explain the functionality or meaning of this : $data['row']

I have this doubt.
// This is my controller
public function profile()
{
$session_data = $this->session->userdata('logged_in');
$uid= $session_data['id'];
$pic = $this->user_model->getImage($uid);
$data['profile_pic']= $pic ;
$data['row'] = $this->user_model->get_user_data($uid);
$test = $this->load->view('profile_view',$data,TRUE);
echo $test;exit;
}
Based on name it seems to be:
public function profile()
{
$session_data = $this->session->userdata('logged_in');//checking user login or not
$uid= $session_data['id'];//getting id from session
$pic = $this->user_model->getImage($uid);//geting user image
$data['profile_pic']= $pic ;//storing image to data array
$data['row'] = $this->user_model->get_user_data($uid);//passing $uid to model and store in data array what row model returns
$test = $this->load->view('profile_view',$data,TRUE);//passing data array to view
echo $test;exit;
}
//model
i am passing user id only
public function get_user_data($uid){
//echo $uid;exit;
$data = array();
$this->db->select('*');
$this->db->from('user');
$this->db->where('id',$uid);
$query = $this->db->get();//checking that $uid data in database
if($query->num_rows() > 0){
$get_user_data=$query->result();
//print_r($get_user_data);exit;
//print_r($items);exit;
return $get_user_data; // if find, return data to controller
}
else{
return FALSE;
}
//return $query->row();
}

Resources