【问题标题】:Call to member function error in CodeIgniterCodeIgniter 中调用成员函数错误
【发布时间】:2012-08-16 15:09:29
【问题描述】:

我正在编写一个使用 ajax 删除表中特定记录的代码。但是每次我尝试删除记录时,都会出现以下错误。

致命错误:在 C:\xampp\htdocs\abcsystem\application\controllers\fuel_types.php 中的非对象上调用成员函数 delete_specific_record() 第 118 行/b>

我尝试了互联网上的几乎所有方法作为解决方案,但我仍然一无所获。我会添加相关代码块供参考。

在 JS 中,

//this function will run when fuel delete button is pressed
$("#fueltypes .delete").click(function(event)
{
    //prevent default action being triggered
    event.preventDefault();

    //store relevant data fields to variables
    var base_url    = $("#base_url").val();
    var fuelTypeId  = $(this).siblings("input[type=hidden]").val();

    //create the data streem
    datastreem = ({fuelTypeId:fuelTypeId});

    $.ajax({
        type:'POST',
        url:base_url+"index.php/fuel_types/ajax_delete_fuel_type",
        data:datastreem,
        success:function(response)
        {
            if(response==1)
            {
                alert("Fuel type deleted");
            }
            else
            {
                alert("Fuel type deletion failed");
            }
        }
    })

});

在控制器(fuel_types.php)中,

class Fuel_types extends CI_Controller
{

    //this function will run when admin try to delete a fuel type
    public function ajax_delete_fuel_type()
    {
        //save ajax sent data into variables
        $fuelTypeId = $this->input->post("fuelTypeId");

        //load vehicle model
        $this->load->model("Vehicles_model");

        //create the recordId array
        $recordId = array('fuel_type_id' => $fuelTypeId);

        //call the function to check if record exists
        $results = $this->Vehicles_model->get_specific_record($recordId);

        //if records available 
        if(($results->num_rows)>0)
        {
            echo "0";
        }
        //if no record is available in the vehicle table
        else
        {                
            //load fuel type model
            $this->load->model("Fuel_types_model");

            //create the recordId array
            $recordId = array('fuel_type_id' => $fuelTypeId);

            //call function to delete records in the model
            $delResults = $this->Fuel_types_model->delete_specific_record($recordId);

            //if record deletion is successful
            if($delResults>0)
            {
                echo("1");
            }
            //if it fails
            else
            {
                echo("0");
            }
        }
    }
}

最后是模型(Fuel_types_model.php),

class Fuel_types_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    //this function will delete record(s) depend on the recordId
    public function delete_specific_record($delRecordId)
    {
        $this->db->where($delRecordId);
        $this->db->delete("fuel_types");

        $delResults = $this->db->affected_rows();

        return $delResults;
    }

}

我真的很困惑,不知道自己做错了什么。仅供参考,我在 config/autoload.php 文件中自动加载了数据库。

我只包含了控制器和模型的相关功能。与此控制器和模型相关的其他功能 - 例如数据检索和更新功能工作得很好。唯一的问题是

   //call function to delete records in the model
   $delResults = $this->Fuel_types_model->delete_specific_record($recordId);

这一行。


(编辑)仅供参考,我在下面包含了完整的错误消息

遇到了 PHP 错误

严重性:通知

消息:未定义的属性: Fuel_types::$fuel_types_model

文件名: controllers/fuel_types.php

行号:118


致命错误:调用成员函数 delete_specific_record() 中的非对象 C:\xampp\htdocs\abcsystem\application\controllers\fuel_types.php 上线 118

【问题讨论】:

  • 尝试将 $this->Fuel_types_model 更改为 $this->fuel_types_model(全部小写
  • @dakshinasd 你确定模型不在子文件夹中,是吗?
  • @Mudshark 是的。模型位于应用程序/模型文件夹中。 (不在子文件夹中)

标签: jquery ajax codeigniter model controller


【解决方案1】:

更改您的模型函数名称更改 delete_specific_record 以删除或其他名称 或者调用控制器,它确实有效,我有同样的问题,但在改变它解决后,不要在函数中使用 delete 关键字并尝试这个函数

public function remove($id)
 {

 $this->db->where('id',$id);// passing your column name,value
   $this->db->delete('fuel_types');
    $newdata = array(
      'msg'  => 'Deleted  Successfully');
       $session=$this->session->set_userdata($newdata);
       return true;
 }

试试这个,真的很管用

【讨论】:

    【解决方案2】:

    在你的控制器中试试这个:

    if($results->num_rows() > 0)
    

    代替:

    if(($results->num_rows)>0)
    

    【讨论】:

      【解决方案3】:

      您的模型中有一个名为delete_specific_record 的方法,但您从控制器中调用了一个同名的方法。您不能在 PHP 中实例化两个具有相同名称的方法。当您更改其中一个名称时会发生什么?

      【讨论】:

      • 他在控制器中加载 Fuel_types_model 类,然后继续使用其中一种方法。没有错。
      • 然而,错误表明他在 fueul_type 控制器中调用了一个名为 delete_specific_record 的函数,而不是模型。还是我在这里遗漏了什么?
      • 我什至尝试调用其他在同一行运行良好的函数。例如:- $this->Fuel_types_model->get_all_records();对于更改的函数名称,我仍然会遇到相同的错误。
      • 等等,让我们看看到底是什么问题。该错误表明变量 $delRecords 不是对象的一部分。你能告诉我你在哪里实例化 $delRecords 吗?
      猜你喜欢
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      相关资源
      最近更新 更多