douyan9398 2019-07-06 11:43
浏览 30
已采纳

Codeigniter未定义的偏移量:提交表单时为0

Undefined offset: 0 error is happening in my edit data form after submitting. Sometimes this works and most of the time it doesnt. any idea where the mistake is happening?

 function data_user_edit($id = ''){

$user = $this->adminmodel->selectdata('user where id_user = "'.$id.'"')->result_array();

            $data = array(
                'title'             => '.:: EVALUASI PROSES BELAJAR MENGAJAR GURU SMA NEGERI 4 BEKASI::. ',
                'titlesistem'       => $this->model->getTitle(),
                'nama'              => $user[0]["nama"],
                'id_user'           => $user[0]["id_user"],
                'status'            => 'edit',
                'username'          => $user[0]["username"],
                'password'          => $user[0]["password"],
                'level'             => $user[0]["level"],
                'tipeakun'          => $user[0]["tipeakun"],
        );

            $this->load->view('admin/header',$data);
            $this->load->view('admin/data_user_form');
            $this->load->view('admin/footer');
  • 写回答

2条回答 默认 最新

  • douhunbei0166 2019-07-06 14:06
    关注

    Imagine a scenario, especially with a public controller, where a user goes to your somepage/data_user_edit manually, and doesn't type in their id. This is the error they will get because $user is returning bool or no rows, thus $user[0] isn't defined.

    First off, if it is a user edit page then I'm quite surprised you are allowing them the option to manually enter their id in to the url, and to presumably change the details of other users simply by changing the url. This should be gotten from a session variable, and they should have to be logged in to see even reach the page.

    Secondly, you should always, as a matter of course, check if num_rows() > 0 before attempting to access your result.

    Thirdly you can just use row_array() for one record instead of result_array(). You wouldn't need $user[0] and could just use $user

    e.g.

    if (is_null($this->session->id)) {
        show_error('not allowed');
    }
    
    $id = $this->session->id;
    
    $query = $this->adminmodel->selectdata('user where id_user = "'.$id.'"');
    
    if ($query->num_rows() !== 1) {
        show_error('no user with id');
    }
    
    $user = $query->row_array();
    
                $data = array(
                    'title'             => '.:: EVALUASI PROSES BELAJAR MENGAJAR GURU SMA NEGERI 4 BEKASI::. ',
                    'titlesistem'       => $this->model->getTitle(),
                    'nama'              => $user["nama"],
                    'id_user'           => $user["id_user"],
                    'status'            => 'edit',
                    'username'          => $user["username"],
                    'password'          => $user["password"],
                    'level'             => $user["level"],
                    'tipeakun'          => $user["tipeakun"],
            );
    
                $this->load->view('admin/header',$data);
                $this->load->view('admin/data_user_form');
                $this->load->view('admin/footer');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置