2

ajax を使用してテーブル内の特定のレコードを削除するコードを書いています。しかし、レコードを削除しようとするたびに、次のエラーが発生します。

致命的なエラー: 118行目のC:\xampp\htdocs\abcsystem\application\controllers\fuel_types.phpの非オブジェクトに対するメンバー関数 delete_specific_record() の呼び出し

私は解決策としてインターネット上のほとんどすべてを試しましたが、まだどこにも行きません。参照用に関連するコード ブロックを追加します。

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


致命的なエラー: 118 行目のC:\xampp\htdocs\abcsystem\application\controllers\fuel_types.phpの非オブジェクトに対するメンバー関数 delete_specific_record() の呼び出し

4

3 に答える 3

0

モデルで名前が付けられたメソッドdelete_specific_recordがありますが、コントローラーから同じ名前のメソッドを呼び出します。PHP では、同じ名前の 2 つのメソッドをインスタンス化することはできません。名前の 1 つを変更するとどうなりますか。

于 2012-08-16T17:12:15.817 に答える
0

コントローラーでこれを試してください:

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

それ以外の:

if(($results->num_rows)>0)
于 2012-08-16T15:16:46.683 に答える