gpt4 book ai didi

jquery - CodeIgniter 中调用成员函数错误

转载 作者:行者123 更新时间:2023-12-01 04:58:57 25 4
gpt4 key购买 nike

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

Fatal error: Call to a member function delete_specific_record() on a non-object in C:\xampp\htdocs\abcsystem\application\controllers\fuel_types.php on line 118

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

在 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");
}
}
})

});

在 Controller (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 文件中自动加载了数据库。

我只包含了 Controller 和模型的相关功能。与该 Controller 和模型相关的其他功能(例如数据检索和更新功能)运行良好。唯一的问题是

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

这一行。

<小时/>

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

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Fuel_types::$fuel_types_model

Filename: controllers/fuel_types.php

Line Number: 118


Fatal error: Call to a member function delete_specific_record() on a non-object in C:\xampp\htdocs\abcsystem\application\controllers\fuel_types.php on line 118

最佳答案

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

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;
}

试试这个,确实有效

关于jquery - CodeIgniter 中调用成员函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com