PHP Error, Undefined index error - php

In my codeigniter project I have this function in my model
function get_rtc_info($id)
{
$query = $this->db->get_where('rtc_info', array('id' => $id));
$query = $query->row_array();
$query = $query['id'].', '.$query['name'].', '.$query['address']; //line# 313
return $query;
}
I get the following error/warning when running the code
A PHP Error was encountered
Severity: Notice
Message: Undefined index: id , name and address
Filename: models/form_model.php
line number 313
I am not sure why I get this undefined index warnings. Can someone guide me on this error and how to resolve it. Thank you in advance

There are no rows in your table rtc_info. This is why you are watching this kind of notice. If you want, not show this notice check your variable with isset() before using
function get_rtc_info($id)
{
$query = $this->db->get_where('rtc_info',array('id' => $id));
$query = $query->row_array();
if(isset($query['id']) && isset($query['name']) && isset($query['address'])) {
$query = $query['id'].', '.$query['name'].', '.$query['address']; //line# 313
return $query;
}else{
return 'no data';
}
}
if these 3 fields are optional you may use following without else block
function get_rtc_info($id)
{
$query = $this->db->get_where('rtc_info',array('id' => $id));
$query = $query->row_array();
if(isset($query['id'])){
$row_id = $query['id'];
}else{
$row_id = 'no id';
}
if(isset($query['name'])){
$name = $query['name'];
}else{
$name = 'no name';
}
if(isset($query['address'])){
$address= $query['address'];
}else{
$address = 'no address';
}
$query = $row_id.', '.$name.', '.$address; //line# 313
return $query;
}
hope now clear

Related

Trying to get property 'email' of non-object

I am getting following error from my code written in Codeigniter when i enter a wrong E-mail.
A PHP Error was encountered Severity: Notice
Message: Trying to get property 'email' of non-object
Filename: controllers/forms.php
Line Number: 26
Backtrace:
File:
E:\Software\XAMPP\xampp\htdocs\ciauth\application\controllers\forms.php
Line: 26 Function: _error_handler
File: E:\Software\XAMPP\xampp\htdocs\ciauth\index.php Line: 315
Function: require_once
Below is the controller
<?php
class Forms extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
$this->load->helper('url');
$this->load->model('user_model', 'auth');
}
public function forgot_pass()
{
if($this->input->post('forgot_pass'))
{
$email=$this->input->post('email');
$this->load->model('user_model', 'auth');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'E-mail', 'required');
if ($this->form_validation->run() == TRUE) {
$que=$this->db->query("select password,email from users where email='$email'");
$row=$que->row();
$user_email=$row->email;
if((!strcmp($email, $user_email)))
{
$pass=$row->password;
$to = $user_email;
$subject = "Password";
$txt = "Your password is $pass .";
$headers = "From: user#testdomain.com" . "\r\n" . "CC: hamza_zon#outlook.com ";
mail($to,$subject,$txt,$headers);
$this->load->view('user/header');
$this->load->view('user/confirm');
$this->load->view('user/footer');
}
else{
$data['error']="Invalid Email ID !";
}
}
else{
$data['error']="Email ID is required !";
}
}
$this->load->view('user/header');
$this->load->view('user/forgot_pass',#$data);
$this->load->view('user/footer');
}
}
?>
you should check before $user_email=$row->email; that the as per your criteria record exist or not, if row is not exist then you will get that error
so you should check as below
$row=$que->row();
if($row) {
$user_email=$row->email;
}
Try like this
$que = $this->db->query("select password,email from users where email=".$email);
if(isset($row) && $row != ''){
$txt = "Your password is" . $pass;
//Coding...
}
You need to handle it like this,
$query = $this->db->query("select password,email from users where email=".$email);
if ($query->num_rows() > 0) {
$row = $query->row();
$user_email = $row->email;
} else {
$user_email = '';
}
//then your next if condition in which you are comparing two strings
$row=$que->row();
$row is probably being returned as NULL or a falsy value.
var_dump($row) to check.
if(!$row)
{
die("user not found");
}
or
if(empty($row))
{
die("user not found");
}
Apply condition as, because in your case it may not getting the result from table for that email,
if (isset($row))
{
Note: row method returns a single result row. If your query has more than one row, it returns only the first row. The result is
returned as an object. Here’s a usage example:
$que=$this->db->query("select password,email from users where email like '$email'");
$row = $que->row();
if (isset($row))
{
$user_email = $row->email;
$user_password = $row->password;
}
Here is concise doc for the same.

How to duplicate category name

I want to make a input box where first check duplicate category. If it find a duplicate category then it return false but if it don't find duplicate then insert new category name into database.
It check duplicate properly and insert category properly but show this error:
Catchable fatal error: Object of class mysqli_result could not be converted to
string in C:\wamp\www\ecommerce\class\category.php on line 23
Call Stack
# Time Memory Function Location
1 0.0006 137488 {main}() ..\catadd.php:0
2 0.0146 176512 Category->insertCat() ..\catadd.php:15
Here is the method that causes the error:
public function insertCat($catName)
{
$catName = $this->fm->validation($catName);
$catName = mysqli_real_escape_string($this->db->link, $catName);
if(empty($catName)){
$msg = 'Cat name can not empty';
return $msg;
}else{
$dcatName = "select * from tbl_category where catName='$catName'";
$rowCount = $this->db->select($dcatName);
$catRow = mysqli_num_rows($rowCount);
if( $catRow > 0 ){ /* <---- Line 23 ----- */
echo 'Cat Name already Exist';
}else{
$query = "INSERT INTO tbl_category (catName) VALUES ('$catName')";
$catResult= $this->db->insert($query);
if($catResult){
$msg = 'Cat name Updated';
return $msg;
return $msg;
}else{
$msg = 'Not updated';
return $msg;
}
}
}
}
The select method looks like:
// Select or Read data
public function select($query){
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows > 0)
{
return $result;
}
else
{
return false;
}
}
On compilation, I got this warning:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\ecommerce\class\category.php on line 22 ( $catRow = mysqli_num_rows($rowCount); is line 22)
Considering your select method looks like this:
public function select($query)
{
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows > 0){
return $result;
} else {
return false;
}
}
...this method already checks number of rows so this row:
$catRow = mysqli_num_rows($rowCount);
...is not needed, furthermore, causes an error if the select() method returns no rows, it returns false which will produce the error when you try to reference a method from a false return. You should be able to just do:
# This will either return the resource or false so you don't need
# to count rows
$rowCount = $this->db->select($dcatName);
if($rowCount) {
EDIT:
One point I should make is that you should probably modify your select() method (and any other query methods you have) to accept an array as a second parameter. That second array should let the method know that you need to bind_param so you can use that feature. This may be an SQL Injection vulnerability:
$dcatName = "select * from tbl_category where catName='$catName'";

Variable returning as undefined in codeigniter?

I am getting the following error message for each variable:
Message: Undefined index: on lines 59, 60, 61, 62
I cannot understand why as it is definitely getting the sessions data and also the databases are setup and their is a user which matches that criteria.
jobs.php(apply function):
public function apply(){
if($this->session->userdata('is_logged_in')){
$this->load->view('header');
$this->load->view('job');
$id = $this->session->userdata('id');
$query = $this->db->get_where('jobseeker_profiles', array('jobseeker_id' => $id));
$user = $query->row_array();
$points = $user['total_points_value'];
$name = $user['name'];
$email = $user['email'];
$jobseeker_profile_id = $user['id'];
$employer_profile_id = $this->input->post('employer_id');
$job_id = $this->input->post('job_id');
$this->db->set('jobseeker_profile_id', $jobseeker_profile_id);
$this->db->set('employer_profile_id', $employer_profile_id);
$this->db->set('job_id', $job_id);
$this->db->set('name', $name);
$this->db->set('email', $email);
$this->db->set('points_value', $points);
if($this->input->post('submit')){
$this->db->insert('applications');
}
redirect('applied-for');
}else{
redirect('login');
}
}
}
Hope fully you can help, thanks!
Have you checked if the 4 database variables you are setting in those lines are rightly declared in the table ? They have to match to work.
do some quick error checking directly from the controller or model like
// check if query did not come back
if ( ! $query = $this->db->get_where('jobseeker_profiles',array('jobseeker_id' => $id)) )
{ echo 'OMFG WHERE IS MY QUERY ???' ; }
else
{ $user = $query->row_array();
echo 'QUERY WORKED, User Name is:' . $user['name'] ;
}
and if this does not work, go back and make sure you are loading the database, that the user/password is correct, etc

Why am I getting all these errors?

Sorry if this seems pretty dumb but simple but I can't seem to figure out why I am getting these errors:
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.9\www\Image Upload\func\user.func.php on line 25
Notice: Undefined index: user_id in C:\Program Files (x86)\EasyPHP-5.3.9\www\Image Upload\register.php on line 37
Here is the code for each file
user.func.php:
function user_exists($email){
$email = mysql_real_escape_string($email);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = `$email`");
return (mysql_result($query, 0) == 1) ? true : false;
}
and here is register.php:
if (isset($_POST['register_email'], $_POST['register_name'], $_POST['register_password'])){
$register_email = $_POST['register_email'];
$register_name = $_POST['register_name'];
$register_password = $_POST['register_password'];
$errors = array();
if(empty($register_email) || empty($register_name) || empty($register_password)){
$errors[] = 'All fields must be filled out';
}
else{
if(filter_var($register_email, FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'Email address not valid';
}
if(strlen($register_email) > 255 || strlen($register_name) > 35 || strlen($register_password) > 35){
$errors[] = 'One or more fields contains too many characters';
}
if(user_exists($register_email) === true){
$errors[] = 'That email has already been registered to another user';
}
}
if(!empty($errors)){
foreach ($errors as $error){
echo $error, '<br />';
}
} else {
$register = user_register($register_email, $register_name, $register_password);
$SESSION['user_id'] = $register;
echo $_SESSION['user_id'];
}
}
Thanks for any help!
-TechGuy24
The query is failing .. it should be email = '$email' (instead of surrounding the second email with backticks).
Please also look up prepared statements and PDO.
mysql_query will return FALSE (a boolean) when it fails and the "resource" you are seeking when it succeeds.
You're using backticks "`" on a value, so your query is failing, use single quotes '
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'");

CodeIgniter function not taking parameters

I defined this function
function login_user($emailusername, $password) { }
the function uses the variables but, when I try to call it using
$this->login->login_user('username', 'password');
it returns an error saying that those variables are undefined in the function.
EDIT: I set the function in a login model
I get these errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: password
Filename: models/login.php
Line Number: 20
line 20: $pass = crypt($password, $hash['password']);
A PHP Error was encountered
Severity: Notice
Message: Undefined index: username
Filename: models/login.php
Line Number: 26
line 26: if ($user_data['username'] == $emailusername || $user_data['email'] == $emailusername) {
A PHP Error was encountered
Severity: Notice
Message: Undefined index: email
Filename: models/login.php
Line Number: 26
line 26: if ($user_data['username'] == $emailusername || $user_data['email'] == $emailusername) {
This is the function
function login_user($emailusername, $password) {
//To initiate session set this cookie
$query = $this->db->query("SELECT password FROM usrs WHERE username = '$emailusername' or email = '$emailusername'");
$hash = $query->result();
$pass = crypt($password, $hash['password']);
$nq = $this->db->query("SELECT usr_id, username, email from usrs WHERE email = '$emailusername' or username = '$emailusername' and password = '$pass'");
$user_data = $nq->result();
if($emailusername&&$password) {
if ($user_data['username'] == $emailusername || $user_data['email'] == $emailusername) {
if($hash['password'] == $pass) {
$_SESSION['login'] = True;
$_SESSION['uid'] = $user_data['usr_id'];
$_SESSION['username'] = $user_data['username'];
return True;
} else
return False;
} else
return False;
} else
return False;
}
This error is not in your variables.
There is no index calldes 'password' in your $hash table;
Check Your $hash table first.
$hash = $query->result();
function return object array consist
$hash->password = '';
If want to get single row and result is to be array use row_array() function
$hash = $query->row_array();
this returns
$hash['password']
The issue originates from this line:
$hash = $query->result();
As per the documentation, result() does the following:
This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop...
So, $hash contains no indices named password, username, or email - hence the error messages you're receiving.
If you're only concerned with the first row returned from that query (which is my assumption), you can use row_array() to return only the first record and as an array, which will make those indices available, like this:
$hash = $query->row_array();
// $hash['password'], $hash['username'], and $hash['email'] are now available
Considering that you are using codeignitor, if the function is in a controller 'Login' then use
$this->login_user('username', 'password');
If the function is in a library, then make sure that you have loaded the library and the name of the instance is 'login'
And if you have included the file directly then just make an object and call the method
$loginObj = new login();
$loginObj->login_user('username', 'password');
Cheers,
Vedant

Categories