Codeigniter:消息:试图获取非对象的属性文件名:edit / edit_nilai.php行号:9

时间:2018-09-07 08:51:58

标签: php codeigniter

Codeigniter:消息:试图获取非对象的属性文件名:edit / edit_nilai.php行号:9

控制器:

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

型号:

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

查看(错误文件edit / edit_nilai)粗体代码为错误行

<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>

2 个答案:

答案 0 :(得分:1)

请检查以下步骤。

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;

答案 1 :(得分:0)

因为模型函数selectNilai($id)不返回任何内容。

可能的原因:

  1. 在表中找不到具有给定$id的匹配值

解决方案:

  1. 尝试检查$id的值,然后手动检查表中是否存在。

最佳做法:

始终在将查询结果发送给处理之前检查返回的结果的值(或以处理错误的方式编写处理机制)。在这种情况下,您实际上可以执行以下操作:

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
}

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