Memperbaiki pesan error pada halaman update data

Saya mengalami kendala pada proses setelah klik Submit update data dan pesan errornya seperti screenshoot dibawah ini.

Minta tolong solusinya teman-teman

Terima kasiherror.png

Controller:

<?php

class Calon extends CI_Controller{

	function __construct() {
		parent::__construct();
		if (!isset($this->session->userdata['username'])) {
			$this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
						  Maaf Anda Belum Login
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
						</div>');
			redirect('administrator/auth');
		}
	}

	public function index()
	{

		$data['calon']	= $this->calon_model->tampil_data('calon')->result();
		$this->load->view('templates_administrator/header');
		$this->load->view('templates_administrator/sidebar');
		$this->load->view('administrator/calon',$data);
		$this->load->view('templates_administrator/footer');
	}

	public function detail($id)
	{
		$data['detail'] = $this->calon_model->ambil_id_calon($id);
		$this->load->view('templates_administrator/header');
		$this->load->view('templates_administrator/sidebar');
		$this->load->view('administrator/calon_detail',$data);
		$this->load->view('templates_administrator/footer');
	}

	public function update($id)
	{
		$where = array('id' => $id);
		$data['calon'] = $this->calon_model->tampil_data('calon')->result();

		$this->load->view('templates_administrator/header');
		$this->load->view('templates_administrator/sidebar');
		$this->load->view('administrator/calon_edit',$data);
		$this->load->view('templates_administrator/footer');
	}

	public function update_calon_aksi()
	{
		$this->_rules();

		if($this->form_validation->run() == FALSE) {
			$this->update();
		}else{
		$id 			= $this->input->post('id');
		$nama_calon 	= $this->input->post('nama_calon');
		$parpol_calon 	= $this->input->post('parpol_calon');
		$jk_calon 		= $this->input->post('jk_calon');
		$lahir_calon 	= $this->input->post('lahir_calon');
		$tgl_calon 		= $this->input->post('tgl_calon');
		$kerja_calon 	= $this->input->post('kerja_calon');
		$dapil_calon 	= $this->input->post('dapil_calon');
		$dapil1		 	= $this->input->post('dapil1');
		$dapil2		 	= $this->input->post('dapil2');
		$dapil3		 	= $this->input->post('dapil3');
		$dapil4		 	= $this->input->post('dapil4');
		$dapil5		 	= $this->input->post('dapil5');
		$dapil6		 	= $this->input->post('dapil6');
		$dapil7		 	= $this->input->post('dapil7');
		$dapil8		 	= $this->input->post('dapil8');
		$dapil9		 	= $this->input->post('dapil9');
		$dapil10		= $this->input->post('dapil10');
		$foto_calon		= $_FILES['userfile']['name'];
			if ($foto_calon){
				$config['upload_path'] = './assets/calon/';
				$config['allowed_types'] = 'jpg|png|gif|tiff|jpeg';

				$this->load->library('upload',$config);

				if($this->upload->do_upload('userfile')){
					$userfile = $this->upload->data('file_name');
					$this->db->set('foto_calon', $userfile);
				}else{
					echo "Gagal Upload";
				}
			}

		$data = array(
			'nama_calon'	=> $nama_calon,
			'parpol_calon'	=> $parpol_calon,
			'jk_calon'		=> $jk_calon,
			'lahir_calon'	=> $lahir_calon,
			'tgl_calon'		=> $tgl_calon,
			'kerja_calon'	=> $kerja_calon,
			'dapil_calon'	=> $dapil_calon,
			'dapil1'		=> $dapil1,
			'dapil2'		=> $dapil2,
			'dapil3'		=> $dapil3,
			'dapil4'		=> $dapil4,
			'dapil5'		=> $dapil5,
			'dapil6'		=> $dapil6,
			'dapil7'		=> $dapil7,
			'dapil8'		=> $dapil8,
			'dapil9'		=> $dapil9,
			'dapil10'		=> $dapil10,
			'foto_calon'	=> $foto_calon,

		);

		$where = array(
			'id' => $id
		);

		$this->calon_model->update_data($where,$data,'calon');
		$this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
						  Profil Calon Berhasil Diubah.
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
						</div>');
		redirect('administrator/calon');
	}
}

	public function _rules()
	{
		$this->form_validation->set_rules('nama_calon','Nama Calon','required',[
			'required' => "Nama Calon Wajib Diisi!"
		]);

		$this->form_validation->set_rules('parpol_calon','Parpol Calon','required',[
			'required' => "Parpol Calon Wajib Diisi!"
		]);

		$this->form_validation->set_rules('jk_calon','Jenis Kelamin','required',[
			'required' => "Jenis Kelamin Wajib Diisi!"
		]);

		$this->form_validation->set_rules('lahir_calon','Tempat Lahir','required',[
			'required' => "Tempat Lahir Wajib Diisi!"
		]);

		$this->form_validation->set_rules('tgl_calon','Tanggal Lahir','required',[
			'required' => "Tanggal Lahir Wajib Diisi!"
		]);

		$this->form_validation->set_rules('kerja_calon','Pekerjaan','required',[
			'required' => "Pekerjaan Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil_calon','Daerah Pemilihan','required',[
			'required' => "Daerah Pemilihan Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil1','Dapil 1','required',[
			'required' => "Dapil 1 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil2','Dapil 2','required',[
			'required' => "Dapil 2 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil3','Dapil 3','required',[
			'required' => "Dapil 3 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil4','Dapil 4','required',[
			'required' => "Dapil 4 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil5','Dapil 5','required',[
			'required' => "Dapil 5 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil6','Dapil 6','required',[
			'required' => "Dapil 6 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil7','Dapil 7','required',[
			'required' => "Dapil 7 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil8','Dapil 8','required',[
			'required' => "Dapil 8 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil9','Dapil 9','required',[
			'required' => "Dapil 9 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('dapil10','Dapil 10','required',[
			'required' => "Dapil 10 Wajib Diisi!"
		]);

		$this->form_validation->set_rules('foto_calon','Foto','required',[
			'required' => "Foto Wajib Diisi!"
		]);

	}
}

Model:

<?php

class Calon_model extends CI_Model{

	public function tampil_data($table)
	{
		return $this->db->get($table);
	}

	public function ambil_id_calon($id)
	{
		$hasil = $this->db->where('id',$id)->get('calon');
		if($hasil->num_rows() > 0){
			return $hasil->result();
		}else{
			return false;
		}
	}

	public function update_data($where,$data,$table)
	{
		$this->db->where($where);
		$this->db->update($table,$data);
	}

}

Form edit:

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">


  </head>
  <body>

<div class="container-fluid">

	<div class="alert alert-success" role="alert">
      <i class="fas fa-user-check"></i> EDIT PROFIL CALON
    </div>

    <?php foreach($calon as $cln) : ?>
        <?php echo form_open_multipart('administrator/calon/update_calon_aksi') ?>

    		<div class="form-group">
    			<label>Nama Calon</label>
    			<input type="hidden" name="id" value="<?php echo $cln->id ?>">
    			<input type="text" name="nama_calon" class="form-control" value="<?php echo $cln->nama_calon ?>">
                <?php echo form_error('nama_calon','<div class="text-danger small ml-3">','</div>') ?>
    		</div>
    		<div class="form-group">
    			<label>Nama Partai Politik</label>
    			<input type="text" name="parpol_calon" class="form-control" value="<?php echo $cln->parpol_calon ?>">
                <?php echo form_error('parpol_calon','<div class="text-danger small ml-3">','</div>') ?>
    		</div>
    		<div class="form-group">
    			<label>Jenis Kelamin</label>
                <select type="text" name="jk_calon" class="form-control" value="<?php echo $cln->jk_calon ?>">
                    <option>LAKI-LAKI</option>
                    <option>PEREMPUAN</option>
                </select>
                <?php echo form_error('jk_calon','<div class="text-danger small ml-3">','</div>') ?>
    		</div>
    		<div class="form-group">
    			<label>Tempat Lahir</label>
    			<input type="text" name="lahir_calon" class="form-control" value="<?php echo $cln->lahir_calon ?>">
                <?php echo form_error('lahir_calon','<div class="text-danger small ml-3">','</div>') ?>
    		</div>
    		<div class="form-group">
    			<label>Tanggal Lahir</label>
    			<input type="date" name="tgl_calon" class="form-control" value="<?php echo $cln->tgl_calon ?>">
                <?php echo form_error('tgl_calon','<div class="text-danger small ml-3">','</div>') ?>
    		</div>
    		<div class="form-group">
    			<label>Pekerjaan</label>
    			<input type="text" name="kerja_calon" class="form-control" value="<?php echo $cln->kerja_calon ?>">
                <?php echo form_error('kerja_calon','<div class="text-danger small ml-3">','</div>') ?>
    		</div>
            <div class="form-group">
                <label>Daerah Pemilihan</label>
                <input type="text" name="dapil_calon" class="form-control" value="<?php echo $cln->dapil_calon ?>">
                <?php echo form_error('dapil_calon','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil1" class="form-control" value="<?php echo $cln->dapil1 ?>">
                <?php echo form_error('dapil1','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil2" class="form-control" value="<?php echo $cln->dapil2 ?>">
                <?php echo form_error('dapil2','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil3" class="form-control" value="<?php echo $cln->dapil3 ?>">
                <?php echo form_error('dapil3','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil4" class="form-control" value="<?php echo $cln->dapil4 ?>">
                <?php echo form_error('dapil4','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil5" class="form-control" value="<?php echo $cln->dapil5 ?>">
                <?php echo form_error('dapil5','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil6" class="form-control" value="<?php echo $cln->dapil6 ?>">
                <?php echo form_error('dapil6','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil7" class="form-control" value="<?php echo $cln->dapil7 ?>">
                <?php echo form_error('dapil7','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil8" class="form-control" value="<?php echo $cln->dapil8 ?>">
                <?php echo form_error('dapil8','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil9" class="form-control" value="<?php echo $cln->dapil9 ?>">
                <?php echo form_error('dapil9','<div class="text-danger small ml-3">','</div>') ?>
            </div>
            <div class="form-group">
                <label>Kabupaten/Kota Dapil</label>
                <input type="text" name="dapil1" class="form-control" value="<?php echo $cln->dapil10 ?>">
                <?php echo form_error('dapil10','<div class="text-danger small ml-3">','</div>') ?>
            </div>
    		<div class="form-group">

                <img src="<?php echo base_url(). 'assets/calon/'.$cln->foto_calon ?>" style="width: 20%"><br><br>

                <label>Foto</label><br>
                    <input type="file" name="userfile" value="<?php echo $cln->foto_calon ?>">
            </div>

    		<button type="submit" class="btn btn-primary">Simpan</button><br><br>

        <?php form_close(); ?>
    <?php endforeach; ?>

</div>

  </body>
</html>
avatar santozaq
@santozaq

3 Kontribusi 1 Poin

Diperbarui 3 tahun yang lalu

4 Jawaban:

<div>Kesalahannya ada di baris ini :</div><pre>if($this-&gt;form_validation-&gt;run() == FALSE) { $this-&gt;update(); &lt;--- Salahnya disini, karena ngga ada parameter id yang dimasukkan }else{</pre><div><br>Coba diubah method update_calon_aksi jadi seperti ini :</div><pre>public function update_calon_aksi() { $this-&gt;_rules(); $id = $this-&gt;input-&gt;post('id');

	if($this-&amp;gt;form_validation-&amp;gt;run() == FALSE) {
		$this-&amp;gt;update($id);  &amp;lt;-- tambahkan $id sebagai parameter
	}else{
	
	$nama_calon 	= $this-&amp;gt;input-&amp;gt;post(&#039;nama_calon&#039;);
	$parpol_calon 	= $this-&amp;gt;input-&amp;gt;post(&#039;parpol_calon&#039;);
	$jk_calon 		= $this-&amp;gt;input-&amp;gt;post(&#039;jk_calon&#039;);
	$lahir_calon 	= $this-&amp;gt;input-&amp;gt;post(&#039;lahir_calon&#039;);
	$tgl_calon 		= $this-&amp;gt;input-&amp;gt;post(&#039;tgl_calon&#039;);
	$kerja_calon 	= $this-&amp;gt;input-&amp;gt;post(&#039;kerja_calon&#039;);
	$dapil_calon 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil_calon&#039;);
	$dapil1		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil1&#039;);
	$dapil2		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil2&#039;);
	$dapil3		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil3&#039;);
	$dapil4		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil4&#039;);
	$dapil5		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil5&#039;);
	$dapil6		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil6&#039;);
	$dapil7		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil7&#039;);
	$dapil8		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil8&#039;);
	$dapil9		 	= $this-&amp;gt;input-&amp;gt;post(&#039;dapil9&#039;);
	$dapil10		= $this-&amp;gt;input-&amp;gt;post(&#039;dapil10&#039;);
	$foto_calon		= $_FILES[&#039;userfile&#039;][&#039;name&#039;];
		if ($foto_calon){
			$config[&#039;upload_path&#039;] = &#039;./assets/calon/&#039;;
			$config[&#039;allowed_types&#039;] = &#039;jpg|png|gif|tiff|jpeg&#039;;

			$this-&amp;gt;load-&amp;gt;library(&#039;upload&#039;,$config);

			if($this-&amp;gt;upload-&amp;gt;do_upload(&#039;userfile&#039;)){
				$userfile = $this-&amp;gt;upload-&amp;gt;data(&#039;file_name&#039;);
				$this-&amp;gt;db-&amp;gt;set(&#039;foto_calon&#039;, $userfile);
			}else{
				echo &quot;Gagal Upload&quot;;
			}
		}

	$data = array(
		&#039;nama_calon&#039;	=&amp;gt; $nama_calon,
		&#039;parpol_calon&#039;	=&amp;gt; $parpol_calon,
		&#039;jk_calon&#039;		=&amp;gt; $jk_calon,
		&#039;lahir_calon&#039;	=&amp;gt; $lahir_calon,
		&#039;tgl_calon&#039;		=&amp;gt; $tgl_calon,
		&#039;kerja_calon&#039;	=&amp;gt; $kerja_calon,
		&#039;dapil_calon&#039;	=&amp;gt; $dapil_calon,
		&#039;dapil1&#039;		=&amp;gt; $dapil1,
		&#039;dapil2&#039;		=&amp;gt; $dapil2,
		&#039;dapil3&#039;		=&amp;gt; $dapil3,
		&#039;dapil4&#039;		=&amp;gt; $dapil4,
		&#039;dapil5&#039;		=&amp;gt; $dapil5,
		&#039;dapil6&#039;		=&amp;gt; $dapil6,
		&#039;dapil7&#039;		=&amp;gt; $dapil7,
		&#039;dapil8&#039;		=&amp;gt; $dapil8,
		&#039;dapil9&#039;		=&amp;gt; $dapil9,
		&#039;dapil10&#039;		=&amp;gt; $dapil10,
		&#039;foto_calon&#039;	=&amp;gt; $foto_calon,

	);

	$where = array(
		&#039;id&#039; =&amp;gt; $id
	);

	$this-&amp;gt;calon_model-&amp;gt;update_data($where,$data,&#039;calon&#039;);
	$this-&amp;gt;session-&amp;gt;set_flashdata(&#039;pesan&#039;, &#039;&amp;lt;div class=&quot;alert alert-danger alert-dismissible fade show&quot; role=&quot;alert&quot;&amp;gt;
					  Profil Calon Berhasil Diubah.
					  &amp;lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;alert&quot; aria-label=&quot;Close&quot;&amp;gt; &amp;lt;span aria-hidden=&quot;true&quot;&amp;gt;&amp;amp;times;&amp;lt;/span&amp;gt;&amp;lt;/button&amp;gt;
					&amp;lt;/div&amp;gt;&#039;);
	redirect(&#039;administrator/calon&#039;);
}

}</pre><div><br>Semoga berhasil!</div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

<div>Terima kasih gan tanggapannya, masih error<br><br>ketika saya masukan $id, error seperti ini:</div><pre><strong>A PHP Error was encountered</strong> Severity: Notice Message: Undefined variable: id Filename: administrator/calon.php Line Number: 51 Backtrace: File: C:\xampp\htdocs\potensial\application\controllers\administrator\calon.php Line: 51 Function: _error_handler File: C:\xampp\htdocs\potensial\index.php Line: 315 Function: require_once </pre><div><br>kalau parameter 'id' justru balik lagi ke halaman form edit dan tidak bisa save.<br><br>Mohon bantuannya, terima kasih</div>

avatar santozaq
@santozaq

3 Kontribusi 1 Poin

Dipost 3 tahun yang lalu

<div>pastikan $id ada di atas if-nya, kaya gini :</div><pre>$id = $this-&gt;input-&gt;post('id');

if($this-&gt;form_validation-&gt;run() == FALSE) { $this-&gt;update($id); &lt;-- tambahkan $id sebagai parameter ...</pre><div><br>kalo masalah balik lagi ke halaman form edit, itu karena emg response dari form_validation-nya gagal, artinya ada form input yg masih kosong / ngga memenuhi rules dari form validation -nya, makanya dia redirect lagi ke halaman form editnya. Silahkan cek lagi di rules form validationnya.</div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

<div>Wah sudah terpecahkan kak,,terima kasih banyak dn tambahan ternyata form_validation foto harus dihapus juga, yang berikut harus dihapus:</div><pre>$this-&gt;form_validation-&gt;set_rules('foto_calon','Foto','required',[ 'required' =&gt; "Foto Wajib Diisi!" ]);</pre>

avatar santozaq
@santozaq

3 Kontribusi 1 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban