error 500 with Call to a member function on bool - php

I am using Fat Free Framework but I got this error when trying to check non-existent data ID with dry() function
my code below will throw Call to a member function dry() on bool error if I supply it with non-existent ID value. It should echo the Empty Cursor echo if the ID not found instead if the error. If I use an ID that is within the database, it will show the id exist.
if(is_numeric($param['id']))
{
$id = $param['id'];
//check if data exist
$data = $data->load($id);
if(!$data->dry())
{
echo 'Data with id:'.$id.' exist';
}
else echo 'Cursor is empty. Data with id:'.$id.' doesnt exist!';
die();
}
else $f3->reroute('/');
how do I check if the ID exists without the error?

If your load() method returns false when the data isn't found, then you should be checking for this before you try any further operations on this value.
I've also changed the test to be
if($data !== false) {
so that if your method returns some other value which looks like false (0, null etc), then this will ensure it only picks up false...
if(is_numeric($param['id']))
{
$id = $param['id'];
//check if data exist
$data = $data->load($id);
if($data !== false) {
echo 'Data with id:'.$id.' exist';
}
else {
echo 'Cursor is empty. Data with id:'.$id.' doesnt exist!';
}
die();
}
else {
$f3->reroute('/');
}

Related

Get true/false codeigniter insert function

every time I use insert function in my model, the return always true, here's my code
public function insert_text($data)
{
return $this->db->table($this->table)->insert($data);
}
call code
if ($this->text->insert_text($data)) {
echo "success";
} else {
echo "failed";
}
even the execution failed (cause of same primary key already exists), code always return "success", how to fix it?
Thanks
check if inserted successfuly
if ($this->db->insert_id()) {
echo "success";
} else {
echo "failed";
}

How to solve this CodeIgniter error message? "Notice: Trying to get property of non-object in CodeIgniter"

I am testing the app I created for possible potential bugs and fixing some security holes into it. It came across my mind to check the database and table of the app exist and if it does it runs the code and if the tables does not exist, it will echo a warning message. However, whenever I am testing a feature, I am getting an error saying, Trying to get property 'project_name' of non-object. Here is the code in my model to which I am testing if the table and column exist:
public function get_project($id){
if ($this->db->field_exists('id','projects') == FALSE) {
$this->db->where('id', $id);
$query = $this->db->get('projects');
if ($query->num_rows() > 0) {
return $query->row();
}return false;
}else{
echo "Table and column does not exist!";
}
}
As you can see, the line where in I am testing if the field exist where the value is FALSE in order for me to display the error warning in the else statement. However, whenever I am doing this, I am getting an error saying Trying to get property 'project_name' of non-object in my views. Here is the code in my view:
<h3>Project Name:<?php echo $project_data->project_name; ?></h3>
<h3>Date Created:<?php echo $project_data->date_created; ?><h3>
<h3>Description:</h3>
<p class='projects-description'>
<?php echo $project_data->project_body; ?>
</p>
you have the wrong testing for the field if it is exist or not,
please change this line from:
if ($this->db->field_exists('id','projects') == FALSE) {
To
if ($this->db->field_exists('id','projects') != FALSE) {
then you will be good to go!
Edit, Surround the above code inside
if ($this->db->table_exists('yourtablename') )
{
// table exists
}
else
{
// table does not exist
}
you have wrong if statement it is true or blank,
please change this line :
if ($this->db->field_exists('id','projects')==FALSE) {
use this line
if ($this->db->field_exists('id','projects')) {
here ..
public function get_project($id){
if ($this->db->field_exists('id','projects')) { //its already check true
$this->db->where('id', $id);
$query = $this->db->get('projects');
if ($query->num_rows() > 0) {
return $query->row();
}return false;
}else{
echo "Table and column does not exist!";
}
}
}

Trying to get property of non-object when username is incorrect using CodeIgniter

I am using password_verify to check the password is correct or not which is working perfectly. My issue is when the user entered the wrong username clicked on login button than I am getting the error that "Trying to get property of non-object".
Instated of error it should display the validation error message.
If I enter the correct username and wrong password than I am getting the proper validation error message(Password is invalid) but If I enter the wrong username and correct password then I am getting the error "Trying to get property of non-object".`
Would you help me out in this?
Controller
public function login_user()
{
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('user_password', 'Password', 'trim|required|xss_clean|callback_check_password_database');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
//redirect(base_url('Student_controller/index'),'refresh');
}
}
/*Checking the login details*/
function check_password_database($user_password)
{
$a_username=$this->input->post('user_email');
if (empty($a_username)) {
$this->form_validation->set_message('check_password_database','Email is Invalid');
}
else{
$password_fetch=$this->access_model->password_fetch($a_username);
if(password_verify($user_password, $password_fetch))
{
redirect('Student_controller/index','refresh');
}
else{
$this->form_validation->set_message('check_password_database','Password is Invalid');
return FALSE;
}
}
}
Model
public function password_fetch($username)
{
$query=$this->db->select('emp_password')
->from('employee_info')
->where('emp_email',$username)
->get();
$result = $query->row();
return $fetch_pass=$result->emp_password;
}
You are doing
return $fetch_pass = $result->emp_password;
without validating if $result is actually an object. If the user does not exist, the $result will not be an object. Consequently, trying to fetch emp_password when $result is not an object, will give you the error message Trying to get property of non-object.
Do this instead:
if (isset($row)) {
return $result->emp_password;
}
Further reference: https://www.codeigniter.com/userguide3/database/results.html
On a side note, an application should respond with a generic error message regardless of whether the user ID or password was incorrect. It should also give no indication to the status of an existing account.
Change your model
public function password_fetch($username)
{
$fetch_pass = false;
$query=$this->db->select('emp_password')
->from('employee_info')
->where('emp_email',$username)
->get();
$result = $query->row();
if (isset($row) && ($row > 0)) {
$fetch_pass=$result->emp_password;
}
return $fetch_pass;
}
You must be trying to access an array directly. Between $a_username and $password_fetch one must be a array which you are directly trying to access.
You must echo both of them to find how values are stored in them.Try printing them with echo $a_username;die; and similarly for the other.
If it is an array try printing echo "<pre>";print_r$(a_username);
Also initialize $fetch_pass="" in model;

Variable not returning a boolean yii 2

I have been working on this code to return a Boolean value, what happens is that the variable gets overwritten with true or false as it looks for a row in the foreach statements. But when I pass it in my view file no value is taken/appears. Can you help me why this is happening and how do I successfully pass the boolean?
View file
echo TestController::Showprerequisites(29005);
Model
$validator;
foreach($prereq as $values){
if(FinishedSubjects::find()->where(['subjectname' => $values['col']])->exists()){
$validator = true;
} else {
$validator = false;
}
}
return $validator;
Controller
public function Showprerequisites($trno){
$model = new TestModel();
return $model->Showprerequisites($trno);
}
EDIT
View update
$preq = TestController::Showprerequisites(29005);
if($preq = true){
echo 'Pre requites completed/read';
} else if($preq = false) {
echo 'Pre requisites not completed/not read';
}
You have logical error in your code, you are assigning value in if condition and then checking, it always return true.
Instead of this:
if($preq = true){
you can check this way too:
if($preq){ // if $preq is true then this line will work.

Can't set error message in Code Igniter 3 when using callback function

I am writing a method that uses POST variables posted by AJAX to add a user to a certain course in the database, but I can't get the callback to work correctly:
public function enroll()
{
$package = array();
$this->load->library('form_validation');
$this->form_validation->set_rules('course', 'Vak', 'required|callback_not_enrolled');
$fields = array("course");
if ($this->form_validation->run($this) === FALSE) {
$errors = array();
$success = array();
foreach ($fields as $field) {
$error = form_error($field);
if ($error !== "") {
$errors[$field] = $error;
} else {
$success[$field] = True;
}
}
$package["field_errors"] = $errors;
$package["field_success"] = $success;
$package["success"] = False;
} else {
$package["database"] = $this->course_model->enroll_user($this->data["user"], $this->input->post("course"));
$package["success"] = True;
}
echo json_encode($package);
}
I wrote the callback not_enrolled to check if the user is not already enrolled to the database. Note that I can't use is_unique because I have to test the combined uniqueness of two fields (so just one or two separate ones don't do the trick) and the id of the user is not included in the form (because it's part of the Code Igniter session).
The callback function:
public function _not_enrolled($course)
{
$exists = ($this->user->is_enrolled($course, $this->data["user_id"]) != False);
if ($exists != False) {
$this->form_validation->set_message("not_enrolled", "Already enrolled");
return False;
} else {
return True;
}
}
And finally the method is_enrolled from the model:
public function is_enrolled($course, $user=False) {
if($user==False){
$user = $this->data["user_id"];
}
$this->db->select()->from("course_participant")->where("user_id", $user)->where("course_id", $course);
$query = $this->db->get();
return($query->num_rows()>0);
}
Through a call to var_dump($this->_not_enrolled($existing_course_id)); I know that both the callback function and the method from the model work, as it correctly returned true.
When I var_dump the $package array or validation_errors() I don't get any validation errors except that it says Unable to access an error message corresponding to your field name Vak(not_enrolled).
I tried removing the initial _ from the function name but that gives me a Server Status 500 error.
I have another setup exactly like this, albeit other database calls, with a callback using the same syntax. This method works perfectly.

Categories