我遇到了这个错误,并一直试图弄清楚连续 3 天出了什么问题,但没有运气:
遇到未捕获的异常
类型:错误
消息:调用未定义的方法 Nsbh::p ost((
文件名: 应用程序\控制器sbh.php
行号:25
NSBH.php
<?php
Class Nsbh extends CI_Controller{
var $API ="";
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->library('session');
$this->load->library('curl');
$this->load->helper('form');
$this->load->helper('url');
}
function index(){
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh'));
$this->load->view('nsbh/list',$data);
}
function create(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
$insert = $this->curl->simple_post($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($insert)
{
$this->session->set_flashdata('hasil','Insert Succeed');
}else
{
$this->session->set_flashdata('hasil','Insert Failed');
}
redirect('nsbh');
}else{
$this->load->view('nsbh/create');
}
}
function edit(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
$update = $this->curl->simple_put($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($update)
{
$this->session->set_flashdata('hasil','Update Succeed');
}else
{
$this->session->set_flashdata('hasil','Update Failed');
}
redirect('nsbh');
}else{
$params = array('id'=> $this->uri->segment(3));
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh',$params));
$this->load->view('nsbh/edit',$data);
}
}
function delete($id){
if(empty($id)){
redirect('nsbh');
}else{
$delete = $this->curl->simple_delete($this->API.'/nsbh', array('id'=>$id), array(CURLOPT_BUFFERSIZE => 10));
if($delete)
{
$this->session->set_flashdata('hasil','Deleted');
}else
{
$this->session->set_flashdata('hasil','Failed');
}
redirect('nsbh');
}
}
}
这就是第 25 行的内容
'id'=> $this->post('id'),
你在'id' => $this->post('id'(中缺少输入
因此,正确的代码如下:
'id' => $this->input->post('id')
您还必须在所有其他地方使用输入。
更改自
$data = array( 'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
自
$data = array('id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
正如您在编辑功能中所做的那样
在构造函数中添加$this->load->library('form_validation');
所以你的构造函数看起来像这样:
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('session');
$this->load->library('curl');
$this->load->library('form_validation');
}