You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter项目报错求助:缺失error_php.php文件及代码排查

解决CodeIgniter中找不到error_php.php文件的警告及数据插入/更新问题

报错信息

Warning: include(C:\xampp\htdocs\test\application\views\errors\html\error_php.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\system\core\Exceptions.php on line 268 
Warning: include(): Failed opening 'C:\xampp\htdocs\test\application\views\errors\html\error_php.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\test\system\core\Exceptions.php on line 268 
Warning: include(C:\xampp\htdocs\test\application\views\errors\html\error_php.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\system\core\Exceptions.php on line 268 
Warning: include(): Failed opening 'C:\xampp\htdocs\test\application\views\errors\html\error_php.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\test\system\core\Exceptions.php on line 268 

控制器代码

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 
#require APPPATH . '/libraries/REST_Controller.php'; 
class Getdetails extends CI_Controller #REST_Controller 
{ 
    public function __construct() { 
        parent::__construct(); 
        $this->load->model('Getusermodel'); 
        $this->load->helper('Utility_helper'); 
    } 

    // insert user data from the api 
    public function insert_user_details() { 
        //curl call for getting user details 
        $url_user = "https://jsonplaceholder.typicode.com/users"; 
        $user_array = curlblock($url_user); 
        //print_r($user_array); 
        $userw_array=array(); 
        foreach ($user_array as $key => $value) { 
            if($key <= 4) { 
                $usrpld=array(
                    "id" => $value['id'] , 
                    "name" => $value['name'], 
                    "username" => $value['username'], 
                    "email" => $value['email'], 
                    "phone" => $value['phone'], 
                ); 
                array_push($userw_array,$usrpld); 
            } 
        } 
        print_r($userw_array); 
    } 
} 

模型代码

<?php 
/** 
 * */ 
class Getusermodel extends CI_Model { 
    public function __construct() { 
        parent::__construct(); 
        $this->load->database(); 
    } 

    public function insert_user(array $payload) { 
        { // 多余的大括号,导致语法错误
            $this->db->where('id',$payload['id']); 
            $query=$this->db->get('userdetails'); 
            $result=$query->result(); 
        } 
        $num_rows=$query->num_rows(); 
        if($num_rows <=1) { 
            //insert 
            $this->db->insert('userdetails',$payload); 
        } else { 
            //update 
            $this->db->where('id', $payload["id"]); 
            $this->db->update("userdetails", array('usrpld' => $payload["userdetails"])); 
        } 
    } 
} 

让我一步步帮你排查和解决这个问题:

1. 先解决error_php.php缺失的警告

这个警告的本质是CodeIgniter捕获到PHP错误后,试图加载默认的错误视图文件但找不到。CI框架默认会在application/views/errors/html/目录下放置error_php.php等错误视图文件,用来格式化展示错误信息。

解决方法二选一:

  • 方法一:恢复缺失的错误视图文件
    从CodeIgniter官方源码包中找到application/views/errors/html/目录下的所有文件,复制到你项目的对应目录里即可。
  • 方法二:调整错误显示配置
    如果你不需要默认的错误页面,可以修改application/config/config.php中的配置,关闭错误显示并开启日志记录:
    $config['display_errors'] = FALSE;
    $config['log_threshold'] = 1; // 1表示记录错误日志,可根据需求调整级别
    

2. 修复触发错误的业务代码问题

CI之所以会尝试加载错误视图,是因为你的代码里存在语法或逻辑错误,我已经在模型代码里找到了明显问题:

(1)修复模型中的语法错误

在insert_user方法里,你多写了一对完全多余的大括号,这会直接导致PHP语法错误:

public function insert_user(array $payload) { 
    { // 删掉这对多余的大括号
        $this->db->where('id',$payload['id']); 
        // ...其他代码
    }

直接删除这对大括号即可。

(2)修复模型中的更新逻辑错误

你的更新语句有两个明显问题:

  • $payload["userdetails"]这个键不存在,应该直接用$payload作为更新数据
  • 判断$num_rows <=1的逻辑不合理:当num_rows=1时说明数据已存在,应该执行更新;只有num_rows=0时才执行插入

修正后的模型insert_user方法:

public function insert_user(array $payload) { 
    $this->db->where('id',$payload['id']); 
    $query=$this->db->get('userdetails'); 
    $num_rows=$query->num_rows(); 

    if($num_rows == 0) { 
        // 数据不存在,执行插入
        $this->db->insert('userdetails',$payload); 
    } else { 
        // 数据已存在,执行更新
        $this->db->where('id', $payload["id"]); 
        $this->db->update("userdetails", $payload); 
    } 
} 

(3)控制器中调用模型方法

你现在的控制器只是组装了数据数组,但没有调用模型的insert_user方法来操作数据库,需要在循环里添加调用:

foreach ($user_array as $key => $value) { 
    if($key <= 4) { 
        $usrpld=array(
            "id" => $value['id'] , 
            "name" => $value['name'], 
            "username" => $value['username'], 
            "email" => $value['email'], 
            "phone" => $value['phone'], 
        ); 
        array_push($userw_array,$usrpld); 
        // 新增:调用模型插入/更新数据
        $this->Getusermodel->insert_user($usrpld); 
    } 
} 

3. 额外检查

确认你的Utility_helper里的curlblock函数能正确处理请求,返回解析后的数组。可以先单独测试这个函数,确保它能从https://jsonplaceholder.typicode.com/users获取到正确的用户数据。


内容的提问来源于stack exchange,提问作者Mustahib Majgaonkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:58:16