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 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题