Call to a member function insert() on null. Codeigniter - php

Hi everyone I am getting the following error when I submit my form for my CI 3 website:
Fatal error: Call to a member function insert() on null
This error is occurring on line 20 which is:
$query = $this->db->insert('temp_subscribed_users', $data);
Here is the full function:
public function add_temp_user($key)
{
echo "hello";
$data = array(
'TEMP_EMAIL' => $this->input->post('email'),
'TEMP_KEY' => $key
);
echo var_dump($data);
$query = $this->db->insert('temp_subscribed_users', $data);
if($query)
{
return true;
}else{
return false;
}
}
I am not sure what it means by null. The table name is correct and I did a var_dump to confirm that the array is being populated. I also made sure that I am getting into the function by echoing "hello" and it is outputting onto the page.
Any help is appreciated thank you!
Additional info: Running using XAMPP localhost.

load database and then call insert function. Codeigniter does not load database automatically for the performance issue.
$this->load->database();
$query = $this->db->insert('temp_subscribed_users', $data);

Well first of all you're not utilizing the MVC model of codeigniter. Controller is for functions, Model is for the database connections.
First autoload your database, If not just put it in the code. But here is how it should look like.
CONTROLLER FUNCTION
public function add_temp_user($key)
{
echo "hello";
$this->load->model('MY_MODEL');
//If you're not autoloading db include the next line
//$this->load->library('database');
$data = array(
'TEMP_EMAIL' => $this->input->post('email'),
'TEMP_KEY' => $key
);
echo var_dump($data);
//If you confirmed the data var dumped
$success = $this->MY_MODEL->insert_to_db($data);
if($success == true)
{
//Do something
}
else
{
//Do something
}
}
MODEL
public function insert_to_db($data)
{
$query = $this->db->insert('temp_subscribed_users', $data);
//
if($query)
{
return true;
}
else
{
return false;
}
}
Make sure the TEMP_EMAIL and TEMP_KEY are the columns in your database and temp_subscribed_users is your table name

Try to solve your problem by getting into the autoload.php in the config folder and add database on the array for libraries, like this: $autoload['libraries'] = array('database');

Please check whether object is created or not.
Check that object is available in that class

Related

NULL Value inserted in Database (Codeigniter)

I Am trying to insert data in DB,But somehow NULL is inserted in DB
Here Is My Controller
foreach($this->input->post('resume_id') as $key =>$value ){
$ResumeInsert[$key]['resume_keyid'] = $Resume['resume_id'][$key];
$ResumeInsert[$key]['employer_name'] = $Resume['employer_name'][$key];
$ResumeInsert[$key]['start_Date'] = $Resume['start_Date'][$key];
$ResumeInsert[$key]['end_date'] = $Resume['end_date'][$key];
$ResumeInsert[$key]['type_id'] = $Resume['type_id'][$key];
$ResumeInsert[$key]['position'] = $Resume['position'][$key];
$ResumeInsert[$key]['responsibility'] = $Resume['responsibility'][$key];
$ResumeInsert[$key]['Skills'] = $Resume['Skills'][$key];
if(isset($Resume['id'][$key]) ){
$Key_Resume__ExistIDs[]=$Resume['id'][$key];
$ResumeUpdate[$key]=$ResumeInsert[$key];
$ResumeUpdate[$key]['resume_id']=$Resume['id'][$key];
unset($ResumeInsert[$key]);
}
else{
$ResumeInsert[$key]['resume_id'] = $GetLastID;
print_r ($ResumeInsert[$key]);exit;
$GetLastID++;
}
}
$idsToDelete='';
if(empty($ResumeInsert) && empty($ResumeUpdate)){
$idsToDelete=array_diff($Key_Resume_IDs,$Key_Resume__ExistIDs);
}
$status=$this->Resume_model->ProcessData($idsToDelete,$ResumeUpdate,$user_id,$ResumeInsert,$imgInsert,$imgUpdate);
redirect('Resume','refresh');
Here Is My Code Of Model
function ProcessData($idsToDelete,$tbl_resumeUpdate,$user_id,$tbl_resumeInsert,$imgInsert,$imgUpdate){
$this->db->trans_start();
if(!empty($idsToDelete)){
$this->delete_tbl_resume($idsToDelete);
}
if(!empty($tbl_resumeUpdate)){
//echo "up";exit;
$this->update_tbl_resume($tbl_resumeUpdate);
}
if(!empty($tbl_resumeInsert)){
//echo "int";exit;
$this->insert_tbl_resume($user_id,$tbl_resumeInsert);
}
if(!empty($imgInsert)){
$this->insert_tbl_file_paths($imgInsert);
}
if(!empty($imgUpdate)){
$this->update_tbl_file_paths($imgUpdate);
}
return $this->db->trans_complete();
}
This is Insert Query
function insert_tbl_resume($id,$arrtbl_resume){
$this->db->insert_batch('tbl_resume', $arrtbl_resume);
}
In Above Code,Null Value inserted In DB.
when i Print above query,it displays blank
Any Help Please?
You should use form_validation library. I'm giving you an example, you can edit and use it.
In autoload.php, edit $autoload['libraries'] = array(); line to:
$autoload['libraries'] = array('form_validation');
Then, use form_validation in your controller file. For example:
$this->form_validation->set_rules('resume_keyid', 'Resume ID', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->index() // if there is an error, user will redirect to this function
}
else
{
$this->Resume_model->ProcessData();
}
Also please use $this->input->post('resume_keyid', TRUE); structure in your model. "TRUE" means "open XSS filter". Because in CI 3, it comes off as default. If you don't want it, just remove. If you use CI 2, you don't need to add "TRUE".
A few suggestions:
1 - Don't use camelization when you name functions. For example; use process_data() instead of processData()
2 - Check CI Form Validation Document for all details (E.g. all references)
3 - I think you can use $this->db->insert();, just create an array and POST it. If you make it, you'll understand what's wrong.

Codeigniter: passing a post variable from one function to another in a controller

Am getting a variable called $msisdn from a view using post in one function (search_results). After doing the processing, I would like to use the same variable in another function(assign_role) currently,I am unable to do that since am getting this error
Severity: Notice Message: Undefined variable: msisdn
. Below is my search_result function where am getting the post data:
public function search_results(){
$msisdn = $this->input->post('search_data');//getting data from the view
if ($msisdn == false){
$this->load->view('add_publisher');
} else{
$this->assign_role($msisdn); //passing the variable to another function
$this->load->model('search_model');
$data['result'] = $this->search_model->search_user($msisdn);
$data1['box'] = $this->search_model->search_select($msisdn);
$result = array_merge($data, $data1);
$this->load->view('add_publisher',$result);
echo json_encode($result);
}
}
I want to use $msisdn from above function in this function below:
public function assign_role($msisdn){
//echo $msisdn['numm'];
$publisher = $this->input->post('select');
if ($publisher == false) {
$this->load->view('add_publisher');
} else {
$data = array(
'publisher' => true,
);
$this->load->model('insert_model');
$data1 = $this->insert_model->search_group($msisdn, $data);
if ($data1 == true) {
echo "Recorded updated succesfully";
$this->load->view('add_publisher');
} else {
echo "Recorded not updated";
$this->load->view('add_publisher');
}
}
}
Please help me to achieve this.
Passing variables from one function to another in the same controller can be achieved by using sessions. In Codeigniter, one can use flashdata like:
$this->session->set_flashdata('info', $MyVariable);
Remember, flashdata is only available for the next server request then it will be automatically cleared.
Then it can be retrieved as:
$var = $this->session->flashdata('info');
If you want to keep the variable for one more server request, you can do this:
$this->session->keep_flashdata('info');
Hope this will help someone faced with the problem.

fatal error in OpenCart: call to undefined function error

I do not want to be asking a question that has already been answered, however I have done a ton of research and I am stuck. Any help would be appreciated.
I am trying to query and add database results to OpenCart's addproduct.tpl
In the MODEL file I have:
public function units() { //function that gets database info and returns it
$unit_variables = $this->db->query("SELECT unit FROM ". DB_PREFIX ."
weight_class_description");
if (!$unit_variables) {
die(mysql_error());
}
else {
foreach ($unit_variables->rows as $temp_var) {
print_r($temp_var['unit'], true);
}
}
}
In the CONTROLLER file I have:
$this->load->model('catalog/product'); //where my function is located
$this->model_catalog_product->units();
$weight_variables = units();
if (isset($this->request->post['weight_variables'])){
$this->data['weight_variables'] = $this->request->post['weight_variables'];
}
In the VIEW I have:
<?php echo $weight_variables ?>
I get the following error:
Call to undefined function units() in /path/to/controller/file on line etc.
Note: When I print_r($temp_var); instead of returning print_R($temp_var, true) and delete these lines of code $weight_variables = units(); if (isset($this->request->post['weight_variables'])){ $this->data['weight_variables'] = $this->request->post['weight_variables'] } in the controller file my model file will display the query results on the addproduct.tpl
units() is a METHOD of your object, yet you're calling it as a standalone regular function:
$weight_variables = units();
^^^^^^^
Shouldn't it be:
$weight_variables = $this->model_catalog_product->units();
instead? And note that as-written, your method doesn't actually return anything, so $weight_variables will simply get assigned null..

Getting the value of a row in CodeIgniter

I am trying to get the value of a row in the database but not working out that well. I not sure if can work like this.
I am just trying to get the value but also make sure it from the group config and the key.
Model
function getTitle() {
return $this->db->get('setting', array(
->where('group' => 'config'),
->where('key' => 'config_meta_title'),
->where('value'=> ) // Want to be able to return any thing in the value
))->row();
}
In the controller I would do this:
function index() {
$data['title'] = $this->document->getTitle();
$this->load->view('sample', $data);
}
First, you have this line set to this:
$data['title'] $this->document->getTitle();
That should be an = assignment for $this->document->getTitle(); like this:
$data['title'] = $this->document->getTitle();
But then in your function you should actually return the setting value from your query with row()->setting:
function getTitle() {
return $this->db->get('setting', array(
->where('group' => 'config'),
->where('key' => 'config_meta_title'),
->where('value'=> ) // Should return this row information but can not.
))->row()->setting;
}
But that said, I am unclear about this:
->where('value'=> ) // Should return this row information but can not.
A WHERE condition is not a SELECT. It is a condition connected to a SELECT that allows you to SELECT certain values WHERE a criteria is met. So that should be set to something, but not really sure what since your code doesn’t provide much details.
Found Solution Working Now. For Getting Single Item From Database In Codeigniter
Loading In Library
function getTitle($value) {
$this->CI->db->select($value);
$this->CI->db->where("group","config");
$this->CI->db->where("key","config_meta_title");
$query = $this->CI->db->get('setting');
return $query->row()->$value;
}
Or Loading In Model
function getTitle($value) {
$this->db->select($value);
$this->db->where("group","config");
$this->db->where("key","config_meta_title");
$query = $this->db->get('setting');
return $query->row()->$value;
}

CodeIgniter $data not passed from Controller to View

my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)

Categories