【问题标题】:Undefined offset: 2 in codeigniter未定义的偏移量:codeigniter 中的 2
【发布时间】:2016-01-07 02:53:52
【问题描述】:

据我所知,这是我的控制器功能,没有错误,但它仍然向我显示此消息。这是什么意思。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

     class User extends CI_Controller {

    public function __construct()
    {
            parent::__construct();
            // Your own constructor code

            //$this->load->model('user_model');
    }

    public function index()
    {
        $this->load->view('user/signup');
    }
    public function registration()
    {

        if($this->input->post())
        {
            $fullname=$this->input->post('fullname');
            $fullnameSplit= explode(" ", $fullname);

            $reg_data = array(
                'membershipid'=>"Ls-".rand(),
                'role'=>1,
                'firstname'=>$fullnameSplit[0],
                'middlename'=>$fullnameSplit[1],
                'lastname'=>$fullnameSplit[2],
                'Company_name'=>$this->input->post('companyname'),
                'email'=>$this->input->post('email'),
                'password'=>sha1($this->input->post('password')),

            );
            print_r($reg_data);
            //$this->user_model->register_user($reg_data);
            //redirect('user/login');

            //echo '<pre>'; print_r($reg_data); die;


        }else 
        {

            //$data['current_page'] = 'Register';
            //$this->load->view('registration',$data);
        }
    }


}

显示此错误。

遇到 PHP 错误 严重性:通知 消息:未定义的偏移量:2 文件名:控制器/User.php 行号:35 回溯: 文件:/var/www/overtribe.com/public_html/listspread/application/controllers/User.php 线路:35 函数:_error_handler 文件:/var/www/overtribe.com/public_html/listspread/index.php 线路:293 函数:require_once

【问题讨论】:

  • 用户控制器中您的 php 标签上方是否有空格?如果是,请删除不需要的空格,也只需评论 print_r($reg_data);
  • 不要依赖用户输入。始终制作虚拟证明代码。最好的方法应该是有更多的输入字段,即$f_name$m_name$l_name,并在控制器中检查这些字段是否为空,然后再使用它们。

标签: php codeigniter


【解决方案1】:

使用这条线

'lastname'=>$fullnameSplit[2],

作为

'lastname'=>isset($fullnameSplit[2]) ? $fullnameSplit[2] : '',

错误 undefined offet:2 表示带有键 [2] 的数组 $fullnameSplit 不存在或未设置。所以你需要检查条件 isset() 以避免该消息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多