Postingan lainnya
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
[Trying to get property 'nim' of non-object]
hi there!
saya punya masalah di sini
<form role="form" class="form-horizontal" action="<?php echo 'cnp_controller/prosesEdit'; ?>" method="post" enctype="multipart/form-data">
<!-- <div class="conatiner"> -->
<div class="card o-hidden border-0 shadow-lg my-5 col-lg-4 mx-auto">
<div class="form-group">
<label for="NIM">NIM</label>
<input type="text" class="form-control" id="nim" name="nim" value=<?php echo $mahasiswa->nim; ?>>
<input type="hidden" class="form-control" name="nim_lama" id=nim_lama>
</div>
<div class="form-group">
<label for="Nama">Nama</label>
<input type="text" class="form-control" id="nama" name="nama" value=<?php echo $mahasiswa->nama; ?>>
<?php echo form_error('nama', '<div style="color:red">', '</div>'); ?>
</div>
<div class="form-group">
<label for="tempat">Tempat Lahir</label>
<input type="text" class="form-control" id="tempat" name="tempat" value=<?php echo $mahasiswa->tempat_lahir; ?>>
</div>
<div class="form-group">
<label for="tgl">Tanggal Lahir (YYYY/mm/dd)</label>
<input type="text" class="form-control" id="tgl" name="tgl" value=<?php echo $mahasiswa->tanggal_lahir; ?>>
</div>
<div class="form-group">
<label for="alamat">Alamat</label>
<input type="text" class="form-control" id="alamat" name="alamat" value=<?php echo $mahasiswa->alamat; ?>>
</div>
<div>
<label for="jk">Jenis Kelamin</label>
<div class="form-check">
<input class="form-check-input" type="radio" name="jk" id="jk" value="Pria">
<label class="form-check-label" for="jk">
Pria
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="jk" id="jk" value="Wanita">
<label class="form-check-label" for="jk">
Wanita
</label>
</div>
</div>
<div class="form-row align-items-center">
<label for="jurusan">Jurusan</label>
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="jurusan" name="jurusan">
<option selected>Choose...</option>
<option value="Administrasi Bisnis">Administrasi Bisnis</option>
<option value="Komputerisasi Akuntansi">Komputerisasi Akuntansi</option>
<option value="Manajemen Informatika">Manajemen Informatika</option>
<option value="Hubungan Masyarakat">Hubungan Masyarakat</option>
</select>
</div>
</div>
<div class="form-row align-items-center">
<label for="angkatan">Angkatan</label>
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="angkatan" name="angkatan">
<option selected>Choose...</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
</div>
<div class="form-group">
<label for="no_hp">No HP</label>
<input type="text" class="form-control" id="no_hp" name="no_hp" value=<?php echo $mahasiswa->no_hp; ?>>
</div>
<div class="form-group">
<label for="no_hp_ortu">No HP Orang Tua</label>
<input type="text" class="form-control" id="no_hp_ortu" name="no_hp_ortu" value=<?php echo $mahasiswa->no_hp_ortu; ?>>
</div>
<div class="form-group">
<label for="foto">Foto</label>
<input type="file" class="form-control-file" id="foto" name="foto">
</div>
<button type="submit" class="btn btn-warning btn-sm my-3 col-lg-3 mx-auto">Update</button>
</div>
<!-- </div> -->
</form>
ini controller nya
public function prosesEdit()
{
$foto = $_FILES['foto'];
if ($foto = '') {
} else {
$konfigurasi['upload_path'] = './upload';
$konfigurasi['allowed_types'] = 'gif|jpg|png|jpeg';
$konfigurasi['max_size'] = '5048';
$konfigurasi['max_height'] = '1920';
$konfigurasi['max_width'] = '1920';
$this->load->library('upload');
$this->upload->initialize($konfigurasi);
if (!$this->upload->do_upload('foto')) {
echo $this->upload->display_errors();
die();
} else {
$foto = $this->upload->data('file_name');
}
}
$this->form_validation->set_rules('nim', 'NIM', 'required|is_unique[mahasiswa.nim]', ['is_unique' => 'The NIM has already registered']);
$this->form_validation->set_rules('nama', 'Nama', 'required');
$data['nim'] = $this->input->post('nim');
$data['nama'] = $this->input->post('nama');
$nim_lama = $this->input->post('nim_lama');
$data['tempat_lahir'] = $this->input->post('tempat');
$data['tanggal_lahir'] = $this->input->post('tgl');
$data['alamat'] = $this->input->post('alamat');
$data['jenis_kelamin'] = $this->input->post('jk');
$data['jurusan'] = $this->input->post('jurusan');
$data['angkatan'] = $this->input->post('angkatan');
$data['no_hp'] = $this->input->post('no_hp');
$data['no_hp_ortu'] = $this->input->post('no_hp_ortu');
$data['foto'] = $foto;
var_dump($data);
$this->cnp_model->editMahasiswa($nim_lama, $data);
$this->session->set_flashdata('message', 'Update Record Success');
redirect(site_url('cnp_controller/mahasiswa'));
}
masalahnya selalu muncul message
[Trying to get property 'nim' of non-object]
A PHP Error was encountered
Severity: Notice
Message: Trying to get property 'nim' of non-object
Filename: views/edit_mahasiswa.php
Line Number: 55
Backtrace:
File: C:\xampp\htdocs\belajarCI\application\views\edit_mahasiswa.php
Line: 55
Function: _error_handler
File: C:\xampp\htdocs\belajarCI\application\controllers\cnp_controller.php
Line: 102
Function: view
line 55 itu yang ini di edit_mahasiswa
<input type="text" class="form-control" id="nim" name="nim" value=<?php echo $mahasiswa->nim; ?>>
line 102 di cnp_controller
$this->load->view('edit_mahasiswa', $data);
Tanggapan
Hallo ka ferro, boleh dilampirkan script controller untuk view edit_mahasiswa? sepertinya ada yang salah saat mengirimkan $data['mahasiswanya']
3 Jawaban:
<pre>public function editMahasiswa($nim) { $data['mahasiswa'] = $this->cnp_model->selectByNim($nim); $this->load->view('edit_mahasiswa', $data); }
public function prosesEdit()
{
$foto = $_FILES['foto'];
if ($foto = '') {
} else {
$konfigurasi['upload_path'] = './upload';
$konfigurasi['allowed_types'] = 'gif|jpg|png|jpeg';
$konfigurasi['max_size'] = '5048';
$konfigurasi['max_height'] = '1920';
$konfigurasi['max_width'] = '1920';
$this-&gt;load-&gt;library('upload');
$this-&gt;upload-&gt;initialize($konfigurasi);
if (!$this-&gt;upload-&gt;do_upload('foto')) {
echo $this-&gt;upload-&gt;display_errors();
die();
} else {
$foto = $this-&gt;upload-&gt;data('file_name');
}
}
$this-&gt;form_validation-&gt;set_rules('nim', 'NIM', 'required|is_unique[mahasiswa.nim]', ['is_unique' =&gt; 'The NIM has already registered']);
$this-&gt;form_validation-&gt;set_rules('nama', 'Nama', 'required');
$data['nim'] = $this-&gt;input-&gt;post('nim');
$data['nama'] = $this-&gt;input-&gt;post('nama');
$data['tempat_lahir'] = $this-&gt;input-&gt;post('tempat');
$data['tanggal_lahir'] = $this-&gt;input-&gt;post('tgl');
$data['alamat'] = $this-&gt;input-&gt;post('alamat');
$data['jenis_kelamin'] = $this-&gt;input-&gt;post('jk');
$data['jurusan'] = $this-&gt;input-&gt;post('jurusan');
$data['angkatan'] = $this-&gt;input-&gt;post('angkatan');
$data['no_hp'] = $this-&gt;input-&gt;post('no_hp');
$data['no_hp_ortu'] = $this-&gt;input-&gt;post('no_hp_ortu');
$data['foto'] = $foto;
$nim_lama = $this-&gt;input-&gt;post('nim_lama');
var_dump($nim_lama);
$this-&gt;cnp_model-&gt;editMahasiswa($nim_lama, $data);
$this-&gt;session-&gt;set_flashdata('message', 'Update Record Success');
redirect(site_url('cnp_controller/mahasiswa'));
}</pre>
<div>model</div><pre>public function selectByNim($nim) { $this->db->distinct(); $this->db->select('*'); $this->db->from('mahasiswa'); $this->db->where('nim', $nim); return $this->db->get()->row(); } public function editMahasiswa($nim, $data) { $this->db->where('nim', $nim); $this->db->update('mahasiswa', $data); }</pre>
<div>Untuk model bisa langsung dipersingkat aja<br><br></div><pre>public function selectByNim($nim){ return $this->db ->get_where('nim',$nim) ->row() }</pre><div><br>Untuk masalah trying to get property 'nim' of non-object, bisa dicek struktur table nya untuk penulisan nama kolom nim nya seperti apa, karena untuk pemanggilan object itu case sensitive jadi harus disesuaikan dengan nama kolom nya</div>