我正在编写一个使用 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
文件名:控制器/fuel_types.php
行号:118
致命错误:在第118 行的C:\xampp\htdocs\abcsystem\application\controllers\fuel_types.php中的非对象上调用成员函数 delete_specific_record()