PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] Codeigniter Undefined offset: 0 when submitting form

 January 28, 2022     codeigniter, php     No comments   

Issue

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

Solution

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


Answered By - Alex
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing