简体   繁体   中英

Codeigniter Undefined offset: 0 when submitting form

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');

检查$user是否$user空,并检查它是否对您的代码有效

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

eg

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM