An uncaught Exception was encountered, Call to undefined method - php

I'm having this error and been trying to figure whats wrong for like 3 days straight with no luck:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined method Nsbh::post()
Filename: application\controllers\nsbh.php
Line Number: 25
nsbh.php
<?php
Class Nsbh extends CI_Controller{
var $API ="";
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->library('session');
$this->load->library('curl');
$this->load->helper('form');
$this->load->helper('url');
}
function index(){
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh'));
$this->load->view('nsbh/list',$data);
}
function create(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
$insert = $this->curl->simple_post($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($insert)
{
$this->session->set_flashdata('hasil','Insert Succeed');
}else
{
$this->session->set_flashdata('hasil','Insert Failed');
}
redirect('nsbh');
}else{
$this->load->view('nsbh/create');
}
}
function edit(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
$update = $this->curl->simple_put($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($update)
{
$this->session->set_flashdata('hasil','Update Succeed');
}else
{
$this->session->set_flashdata('hasil','Update Failed');
}
redirect('nsbh');
}else{
$params = array('id'=> $this->uri->segment(3));
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh',$params));
$this->load->view('nsbh/edit',$data);
}
}
function delete($id){
if(empty($id)){
redirect('nsbh');
}else{
$delete = $this->curl->simple_delete($this->API.'/nsbh', array('id'=>$id), array(CURLOPT_BUFFERSIZE => 10));
if($delete)
{
$this->session->set_flashdata('hasil','Deleted');
}else
{
$this->session->set_flashdata('hasil','Failed');
}
redirect('nsbh');
}
}
}
And this is what's on line 25
'id'=> $this->post('id'),

You are missing input in 'id' => $this->post('id')
So, the right code is as follow:
'id' => $this->input->post('id')
You have to use input on all other places also.

change from
$data = array( 'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
to
$data = array('id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
As you already did in edit function

In the constructor add $this->load->library('form_validation');
So your constructor looks like this:
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('session');
$this->load->library('curl');
$this->load->library('form_validation');
}

Related

Undefined array key "id" in CodeIgniter 4

Undefined array key "id" in CodeIgniter 4
enter image description here
public function update($id)
{
$slug = url_title($this->request->getVar('nama_rak'), '-', true);
$this->rakModel->save([
'id' => $id,
'nama_rak' => $this->request->getVar('nama_rak'),
'slug' => $slug,
'detail_rak' => $this->request->getVar('detail_rak'),
'created_at' => $this->request->getVar('created_at'),
'updated_at' => $this->request->getVar('updated_at')
]);
return redirect()->to('/master/master_rak')->with('pesan', 'Data Berhasil diubah!');
}
this is the construct
class Master extends BaseController
{
protected $rakModel;
public function __construct()
{
$this->rakModel = new RakModel();
}
// Pengadaan Buku
public function master_rak()
{
$data = [
'pages' => 'Master Data',
'title' => 'Data Rak',
'rak' => $this->rakModel->getRak()
];
echo view("master/master_rak", $data);
}

Model & Controller error on my website project

Please help me, I cannot access my code it always shows the error:
An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Quotation_model::addModels(), 1 passed in C:\xampp\htdocs\sangil\application\controllers\Quotation.php on line 126 and exactly 2 expected
Filename: C:\xampp\htdocs\sangil\application\models\Quotation_model.php
Line Number: 122
Backtrace:
File: C:\xampp\htdocs\sangil\application\controllers\Quotation.php
Line: 126
Function: addModels
File: C:\xampp\htdocs\sangil\index.php
Line: 315
Function: require_once
This is the code:
Controller: Quotation.php
public function addQuotation()
{
$this->form_validation->set_rules('date', 'Date', 'trim|required');
$this->form_validation->set_rules('reference_no', 'Reference No', 'trim|required');
if ($this->form_validation->run() == false) {
$this->add();
} else {
$warehouse_id = $this->input->post('warehouse', true);
$data = array(
"date" => $this->input->post('date'),
"reference_no" => $this->input->post('reference_no'),
"warehouse_id" => $this->input->post('warehouse'),
"customer_id" => $this->input->post('customer'),
"biller_id" => $this->input->post('biller'),
"total" => $this->input->post('grand_total'),
"discount_value" => $this->input->post('total_discount'),
"tax_value" => $this->input->post('total_tax'),
"note" => $this->input->post('note'),
"shipping_city_id" => $this->input->post('city'),
"shipping_state_id" => $this->input->post('state'),
"shipping_country_id" => $this->input->post('country'),
"shipping_address" => $this->input->post('address'),
"shipping_charge" => $this->input->post('shipping_charge'),
"internal_note" => $this->input->post('internal_note'),
"mode_of_transport" => $this->input->post('mode_of_transport'),
"transporter_name" => $this->input->post('transporter_name'),
"transporter_code" => $this->input->post('transporter_code'),
"vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
"user" => $this->session->userdata('user_id')
);
if ($quotation_id = $this->quotation_model->addModel($data)) {
$log_data = array(
'user_id' => $this->session->userdata('user_id'),
'table_id' => $quotation_id,
'message' => 'Quotation Inserted'
);
$this->log_model->insert_log($log_data);
$sales_item_data = $this->input->post('table_data', true);
$js_data = json_decode($sales_item_data);
foreach ($js_data as $key => $value) {
if ($value == null) {
} else {
$product_id = $value->product_id;
$quantity = $value->quantity;
$data = array(
"product_id" => $value->product_id,
"quantity" => $value->quantity,
"price" => $value->price,
"gross_total" => $value->total,
"discount_id" => $value->discount_id,
"discount_value" => $value->discount_value,
"discount" => $value->discount,
"tax_id" => $value->tax_id,
"tax_value" => $value->tax_value,
"tax" => $value->tax,
"quotation_id" => $quotation_id
);
//$this->sales_model->checkProductInWarehouse($product_id,$quantity,$warehouse_id);
if ($this->quotation_model->addQuotationItem($data, $product_id, $warehouse_id, $quantity)) {
} else {
}
}
}
redirect('quotation/view/' . $quotation_id);
} else {
redirect('quotation', 'refresh');
}
}
}
Model: Quotation_model.php
public function addModel($data, $invoice)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
} else {
return FALSE;
}
}
you create model with 2 input
$data and $invoice
public function addModel($data, $invoice)
but only give 1 input in the controller when you call the model
$this->quotation_model->addModel($data)
delete the $invoice in your model if you dont need it, or add one more input when you call it from the controler.
public function addModel($data)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
}
return false;
}
Controller: Quotation.php
$quotation_id = $this->quotation_model->addModel($data);
so in your Model: Quotation_model.php
public function addModel($data)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
} else {
return FALSE;
}
}

last inserted id in Php codeIgniter

In my model I have return below function for get record id
function getLastInserted()
{
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration");
return $query->result();
}
Now I want to pass that ID to mycontroller for record insertion
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
Now I want to access model function in controller and pass record id in data function How I do ??
set return in model and in controller write
$insert_id=$this->db->insert_id();
In Controller load your model first using
$this->load->model('model_name');
Then call to model's getLastInserted() function.
$ID = $this->model_name->getLastInserted();//you will get id here
In model return $query->row()->userid; insead return of $query->result().
Then modify the controller:
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $this->model_name->getLastInserted(),
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
hi i worked out the solution on last inserted id dear here the code:
controller:
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
model:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
model:
public function newregistration($sid)
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $sid,
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}

Parsing data from 1 method to another method in codeigniter

On my controller i have this method.
public function abc() {
$data = array(
'firstname' => 'abc',
'username' => 'abc#gmail.com',
'password' => random_string('alnum', 6),
'email' => 'abc#gmail.com'
);
echo $data['firstname'];
}
i want to pass that array data ($data) on another function
public function abcx() {
$this->abc();
//print the variable data from abc method
}
How to do this ?
First Function :
public function abc() {
$data = array(
'firstname' => 'abc',
'username' => 'abc#gmail.com',
'password' => random_string('alnum', 6),
'email' => 'abc#gmail.com'
);
return $data['firstname'];
}
Another function
public function abcx() {
$data = $this->abc();
//print the variable data from abc method
echo $data;
}

Error Fatal error: Call to a member function insert() on a non-object

I'm using a callback after inserting in the database, to pass values to this other table, i think the method i used its fine. But it gives me this error when its inserting to the other table in the database, probably in my model, or something in the controller im kind of clueless in this error, can someone please help me.
Before someone say its to put $this->load->database(); its already in my autoload in config file :)
Fatal error: Call to a member function insert() on a non-object
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Finances::$inventory_model
Filename: controllers/finances.php
Line Number: 125
CONTROLLER:
public function inventory_management($post_array,$primary_key)
{
$this->load->model('inventory_model');
if($post_array['id_expense']!=null){
$type = 'add';
$id_exp_detail = $primary_key;
$result = $this->inventory_model->insert([
'id_exp_detail' => $id_exp_detail,
'name' => $post_array['brand'],
'quantity' => $post_array['item_quantity'],
'lote' => $post_array['package_code'];
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}else if ($post_array['name']!=null){
$type = 'sub';
$id_treatment = $primary_key;
$result = $this->inventory_model->insert([
'id_treatment' => $id_treatment,
'name' => $post_array['name'],
'quantity' => $post_data['recomended_dose'],
'lote' => $post_array['id_package'],
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}else{
$type = 'sub';
$id_fertilization = $primary_key;
$result = $this->inventory_model->insert([
'id_fertilization' => $id_fertilization,
'name' => $post_data['name'],
'quantity' => $post_data['quantity'],
'lote' => $post_data['id_package'],
'type' => $type,
'date' => date(),
'id_user' => $this->session->userdata('id_user'),
'id_entity' => $this->session->userdata('id_entity')
]);
}
if($result){
echo "validation Complete";
}
}
MODEL
class CRUD_model extends CI_Model
{
protected $_table = null;
protected $_primary_key = null;
// ------------------------------------------------------------------------
public function __construct()
{
parent::__construct();
}
public function insert($data)
{
$this->db->insert($this->_table, $data);
// return $this->db->insert_id();
}
MODEL:
class inventory_model extends CRUD_model
{
protected $_table = 'inventory_management';
protected $_primary_key = 'id';
// ------------------------------------------------------------------------
public function __construct()
{
parent::__construct();
}
// ------------------------------------------------------------------------
}
here is the wrong you call wrong model file
$this->load->model('inventory_model');
it should be
$this->load->model('crud_model');
and you use wrong way to pass array in function
$param = array('id_exp_detail' => $id_exp_detail, 'name' => $post_array['brand'], 'quantity' => $post_array['item_quantity'], 'lote' => $post_array['package_code'], 'type' => $type, 'date' => date(), 'id_user' => $this->session->userdata('id_user'), 'id_entity' => $this->session->userdata('id_entity'));
$result = $this->inventory_model->insert($param);

Categories