简体   繁体   中英

Codeigniter: Message: Trying to get property of non-object Filename: edit/edit_nilai.php Line Number: 9

Codeigniter: Message: Trying to get property of non-object Filename: edit/edit_nilai.php Line Number: 9

controller:

$this->load->model('M_data');
$data['f']=$this->M_data->selectNilai($this->uri->segment(6));
$this->load->view('kurikulum/penilaian/penilaian/edit/edit_nilai',$data);

model:

public function selectNilai($id)
{
    $this->db->where('id_nilai_siswa', $id);
    return $this->db->get('nilai_siswa')->row(); 
}

views (the error file edit/edit_nilai) the bold code is the error line

<html>
<body>
  <div class="modal-dialog " >
    <div class="modal-header">
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
     <h2 class="modal-title">Edit Nilai Siswa</h4>
    </div>
   <div class="modal-content"> 
    **<form class="form-horizontal formgrup "  action="<?php echo base_url('penilaian/ubah_nilai/'.$f->id_nilai_siswa); ?>" method="post" >**
      <div class="bigbox-mapel" > 
        <div class="box-mapel">
        </div>
        <div class="modal-footer">
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button class="btn btn-success" type="submit">Submit</button>
              <button class="btn btn-danger" data-dismiss="modal" href="#lihatkategori" data-toggle="tab">Back</button>
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
</div>

</body>
</html>

Please check below steps.

1. Please check id exist or not.
2. Please print your result.
3. Please try using below method.
public function selectNilai($id)
{        
    echo $id;

    $this->db->where('id_nilai_siswa', $id);
    return $this->db->get('nilai_siswa')->result(); 
}

echo $f[0]->id_nilai_siswa;

Because your model function selectNilai($id) is returning nothing.

Possible cause:

  1. No matching value found in the table with given $id

Solution:

  1. Try to check the value of $id , and manually check if exists in the table.

Best practice:

Always check the value of returned query result before sending it for processing (or write your processing mechanism in a way to handle error). In this case what you can actually do is something like below:

public function selectNilai($id){
    $this->db->select('*')->from('nilai_siswa')->where('id_nilai_siswa', $id);
    $query = $this->db->get();
    if($query->num_rows()){
        //found some data, handle it
        return $query->result();
    } else {
        //no matching data found
        return FALSE;
    }
}

OR

public function selectNilai($id){
    $this->db->where('id_nilai_siswa', $id);
    $data = $this->db->get('nilai_siswa')->row(); 
    if(isset($data)) return $data; //data found, return it for further processing
    else return FALSE; //no data found, handle the case
}

Ref: https://www.codeigniter.com/userguide3/database/results.html#result-rows

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