[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);
avatar ferropesa
@ferropesa

3 Kontribusi 0 Poin

Diperbarui 3 tahun yang lalu

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-&gt;cnp_model-&gt;selectByNim($nim); $this-&gt;load-&gt;view('edit_mahasiswa', $data); }

public function prosesEdit()
{
		$foto = $_FILES[&#039;foto&#039;];
		if ($foto = &#039;&#039;) {
		} else {
			$konfigurasi[&#039;upload_path&#039;] = &#039;./upload&#039;;
			$konfigurasi[&#039;allowed_types&#039;] = &#039;gif|jpg|png|jpeg&#039;;
			$konfigurasi[&#039;max_size&#039;] = &#039;5048&#039;;
			$konfigurasi[&#039;max_height&#039;] = &#039;1920&#039;;
			$konfigurasi[&#039;max_width&#039;] = &#039;1920&#039;;

			$this-&amp;gt;load-&amp;gt;library(&#039;upload&#039;);
			$this-&amp;gt;upload-&amp;gt;initialize($konfigurasi);
			if (!$this-&amp;gt;upload-&amp;gt;do_upload(&#039;foto&#039;)) {
				echo $this-&amp;gt;upload-&amp;gt;display_errors();
				die();
			} else {
				$foto = $this-&amp;gt;upload-&amp;gt;data(&#039;file_name&#039;);
			}
		}
		$this-&amp;gt;form_validation-&amp;gt;set_rules(&#039;nim&#039;, &#039;NIM&#039;, &#039;required|is_unique[mahasiswa.nim]&#039;, [&#039;is_unique&#039; =&amp;gt; &#039;The NIM has already registered&#039;]);
		$this-&amp;gt;form_validation-&amp;gt;set_rules(&#039;nama&#039;, &#039;Nama&#039;, &#039;required&#039;);

		$data[&#039;nim&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;nim&#039;);
		$data[&#039;nama&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;nama&#039;);
		$data[&#039;tempat_lahir&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;tempat&#039;);
		$data[&#039;tanggal_lahir&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;tgl&#039;);
		$data[&#039;alamat&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;alamat&#039;);
		$data[&#039;jenis_kelamin&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;jk&#039;);
		$data[&#039;jurusan&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;jurusan&#039;);
		$data[&#039;angkatan&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;angkatan&#039;);
		$data[&#039;no_hp&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;no_hp&#039;);
		$data[&#039;no_hp_ortu&#039;] = $this-&amp;gt;input-&amp;gt;post(&#039;no_hp_ortu&#039;);
		$data[&#039;foto&#039;] = $foto;
		$nim_lama = $this-&amp;gt;input-&amp;gt;post(&#039;nim_lama&#039;);

		var_dump($nim_lama);
		$this-&amp;gt;cnp_model-&amp;gt;editMahasiswa($nim_lama, $data);
		$this-&amp;gt;session-&amp;gt;set_flashdata(&#039;message&#039;, &#039;Update Record Success&#039;);
		redirect(site_url(&#039;cnp_controller/mahasiswa&#039;));

}</pre>

avatar ferropesa
@ferropesa

3 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

<div>model</div><pre>public function selectByNim($nim) { $this-&gt;db-&gt;distinct(); $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('mahasiswa'); $this-&gt;db-&gt;where('nim', $nim); return $this-&gt;db-&gt;get()-&gt;row(); } public function editMahasiswa($nim, $data) { $this-&gt;db-&gt;where('nim', $nim); $this-&gt;db-&gt;update('mahasiswa', $data); }</pre>

avatar ferropesa
@ferropesa

3 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

<div>Untuk model bisa langsung dipersingkat aja<br><br></div><pre>public function selectByNim($nim){ return $this-&gt;db -&gt;get_where('nim',$nim) -&gt;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>

avatar cyanotic
@cyanotic

9 Kontribusi 3 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban