Call to a member function updat_data() on null - php

I have got this problem, I tried many ways but I couldnt reach a soulution.
this is my main.php code for updation it edits the data but doesnot insert it to update . null error is in updat_data() function. Please help. I am confused.
function get_data($id){
$this->index();
$data['ed']= $this->main_model->get_data($id);
$this->load->view('edit',$data);
}
public function update_data()
{
$id = $this->input->post('id');
$data= array('first_name' =>$this->input->post('first_name') , 'last_name' =>$this->input->post('last_name') );
$this->main_model->updat_data($id,$data);
$this->load->model("main_model");
}
this is model.php code for update..
function get_data($id)
{
//$this->db->where("id", $id);
/// $this->db->update("user",$data);
$this->db->where("id",$id);
$query=$this->db->get("user");
return $query->result();
}
function updat_data($id,$data)
{
$this->db->where('id',$id);
$this->db->update('user',$data);
//return $query->result();
}

try to load the model before you use it in main.php

Related

PHP: can't insert data to database in web hosting, but in local it's working using CI

I'm writing PHP using CI. I inserted data, but got an error like this:
Code:
class Recruit extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('Resume_model');
$this->load->helper(array('form','url'));
}
public function index() {
$this->data['posts'] = $this->Resume_model->get_resume();
$this->load->view('recruit', $this->data);
}
public function resume_form() {
echo 'OK';
$test = $this->input->post('type');
echo 'test:',$test;
echo 'KO';
$save = array(
'type' =>$this->input->post('type'),
'fullname' =>$this->input->post('fullname'),
'tel' =>$this->input->post('tel'),
'location' =>$this->input->post('location')
);
$this->Resume_model->saveResume($save);
redirect(base_url()."recruit");
}
Class Resume_model extends CI_Model {
function saveResume($data) {
{
$this->db->insert('resume', $data);
$resume_id = $this->db->insert_id();
}
return $resume_id;
}
function get_resume() {
$this->db->select("fullname,type");
$this->db->from('resume');
$query = $this->db->get();
return $query->result();
}
function getResume() {
$query = $this->db->query('SELECT * FROM resume');
if($query->num_rows() > 0 ) {
return $query->result();
} else {
return array();
}
}
public function deleteResume($id) {
$this -> db -> where('id', $id);
$this -> db -> delete('resume');
}
Make your fields in the DB to allow NULL input.
The reason is simple, your localhost environment and your online environment are different. Different MySQL configurations or different versions causes these errors.
Sometimes there is a need for putting in an empty record into a reference table for future use or because of the order of your coding event but to fix it you have to either put data into your columns upon inserting or setting your column (type) to 'NULL' or 'NULLABLE' from the DB.

I want to get max id value from table in codeigniter

in model file i write this code:
public function maxcode()
{
$this->db->select (max(ClassID)+1 as ClassID)-> from ('class');
$query=$this->db->get();
if($query->num_rows>0)
{
$row = $query->row_array();
return $row;
}
echo $row['ClassID'];
}
in controller function i write.....this code
public function maxcode()
{
$this->insert->maxcode();
}
Now I understand what your problem is.
You need to transfer this value to the template. This is done in the page controller
Example
$data['ClassID'] = $this->insert->maxcode();
$this->view('tenplate Name', $data);
And used in template
<?php echo $ClassID; ?>

Not Sure How to call Store Procedure in Codeigniter

I am having an issue calling a stored procedure in my model not sure if I am doing it right the screen is coming up blank, also do i need to auto load anything to call a stored procedure?
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return $this->db->insert_id();
}
public function get_employee()
{
$this->db->query("call {storedprocedure Select_employeelist} ");
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return print_r($data);
}
}
To call a procedure from codeigniter
Consider test_proc as your procedure. Then it should be
$this->db->query("call test_proc()");
If you need to to send parameters then
$query = $this->db->query($test_proc,array('id'=>'1','name'=>'test','address'=>'abc'));
To see the result then
print_r($query);

update data in codeigniter

my code update is not working. i have some code in controller
function update($id) {
$this->load->model("Model_mahasiswa");
$data['nim']=$_POST['nim'];
$data['nama']=$_POST['nama'];
$data['alamat']=$_POST['alamat'];
$result=$this->Model_mahasiswa->edit($id, $data);
if($result){
header("location: http://localhost/si_akademik/index.php/mahasiswa/");
}
}
and in modal code
public function edit($id, $data)
{
$this->db->where('id', $id);
$result = $this->db->update('mahasiswa', $data);
return $result;
}
but error: Call to a member function edit() on a non-object
can you help me solve this problem?
Modify you model function :
public function edit($id, $data)
{
$this->load->database();
$this->db->where('id', $id);
$result = $this->db->update('mahasiswa', $data);
return $result;
}

insert data into database with codeigniter

Trying to insert a row into my database with CodeIgniter.
My database table is Customer_Orders and the fields are CustomerName and OrderLines. The variables are being submitted correctly.
My Controller is( sales.php ):
function new_blank_order_summary()
{
$data = array(
'OrderLines'=>$this->input->post('orderlines'),
'CustomerName'=>$this->input->post('customer')
);
$this->sales_model->order_summary_insert($data);
$this->load->view('sales/new_blank_order_summary');
}
My Model is( sales_model.php ):
function order_summary_insert($data){
$this->db->insert('Customer_Orders',$data);
}
Whilst the view loads correctly, no data is inserted into the database.
Any ideas as to why not?
Try this in your model:
function order_summary_insert()
$OrderLines=$this->input->post('orderlines');
$CustomerName=$this->input->post('customer');
$data = array(
'OrderLines'=>$OrderLines,
'CustomerName'=>$CustomerName
);
$this->db->insert('Customer_Orders',$data);
}
Try to use controller just to control the view and models always post your values in model. it makes easy to understand.
Your controller will be:
function new_blank_order_summary() {
$this->sales_model->order_summary_insert($data);
$this->load->view('sales/new_blank_order_summary');
}
It will be better for you to write your code like this.
In your Controller Write this code.
function new_blank_order_summary() {
$query = $this->sales_model->order_summary_insert();
if($query) {
$this->load->view('sales/new_blank_order_summary');
} else {
$this->load->view('sales/data_insertion_failed');
}
}
and in your Model
function order_summary_insert() {
$orderLines = trim(xss_clean($this->input->post('orderlines')));
$customerName = trim(xss_clean($this->input->post('customer')));
$data = array(
'OrderLines'=>$orderLines,
'CustomerName'=>$customerName
);
$this->db->insert('Customer_Orders',$data);
return ($this->db->affected_rows() != 1) ? false : true;
}
function saveProfile(){
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$post_data = array('firstname'=> $firstname,'lastname'=>$lastname);
$this->db->insert('posts',$post_data);
return $this->db->insert_id();
}
View
<input type="text" name="name"/>
<input type="text" name="class"/>
Controller
function __construct()
{
parent:: __construct();
$this->load->Model('Model');
}
function index()
{
$this->load->view('view');
}
function user(){
if (isset($_POST['submit'])){
$data = array('name'=>$_POST['name'],
'class'=>$_POST['class']);
$this->Model->insert($data);
}
}
Model
function insert($data)
{
$this->db->insert('table_name',$data);
return true;
}
Based on what I see here, you have used lowercase fieldnames in your $data array, and uppercase fieldnames in your database table.
function order_summary_insert()
$OrderLines=$this->input->post('orderlines');
$CustomerName=$this->input->post('customer');
$data = array(
'OrderLines'=>$OrderLines,
'CustomerName'=>$CustomerName
);
$this->db->insert('Customer_Orders',$data);
}
Check your controller:
function order()
$OrderLines = $this->input->post('orderlines');
$CustomerName = $this->input->post('customer');
$data = array(
'OrderLines' => $OrderLines,
'CustomerName' =>$CustomerName
);
$this->db->insert('Customer_Orders', $data);
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cnt extends CI_Controller {
public function insert_view()
{
$this->load->view('insert');
}
public function insert_data(){
$name=$this->input->post('emp_name');
$salary=$this->input->post('emp_salary');
$arr=array(
'emp_name'=>$name,
'emp_salary'=>$salary
);
$resp=$this->Model->insert_data('emp1',$arr);
echo "<script>alert('$resp')</script>";
$this->insert_view();
}
}
for more detail visit:
http://wheretodownloadcodeigniter.blogspot.com/2018/04/insert-using-codeigniter.html
Just insert $this->load->database(); in your model:
function order_summary_insert($data){
$this->load->database();
$this->db->insert('Customer_Orders',$data);
}

Categories