Postingan lainnya
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">×</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">×</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>
4 Jawaban:
<div>Kesalahannya ada di baris ini :</div><pre>if($this->form_validation->run() == FALSE) { $this->update(); <--- 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->_rules(); $id = $this->input->post('id');
if($this-&gt;form_validation-&gt;run() == FALSE) {
$this-&gt;update($id); &lt;-- tambahkan $id sebagai parameter
}else{
$nama_calon = $this-&gt;input-&gt;post('nama_calon');
$parpol_calon = $this-&gt;input-&gt;post('parpol_calon');
$jk_calon = $this-&gt;input-&gt;post('jk_calon');
$lahir_calon = $this-&gt;input-&gt;post('lahir_calon');
$tgl_calon = $this-&gt;input-&gt;post('tgl_calon');
$kerja_calon = $this-&gt;input-&gt;post('kerja_calon');
$dapil_calon = $this-&gt;input-&gt;post('dapil_calon');
$dapil1 = $this-&gt;input-&gt;post('dapil1');
$dapil2 = $this-&gt;input-&gt;post('dapil2');
$dapil3 = $this-&gt;input-&gt;post('dapil3');
$dapil4 = $this-&gt;input-&gt;post('dapil4');
$dapil5 = $this-&gt;input-&gt;post('dapil5');
$dapil6 = $this-&gt;input-&gt;post('dapil6');
$dapil7 = $this-&gt;input-&gt;post('dapil7');
$dapil8 = $this-&gt;input-&gt;post('dapil8');
$dapil9 = $this-&gt;input-&gt;post('dapil9');
$dapil10 = $this-&gt;input-&gt;post('dapil10');
$foto_calon = $_FILES['userfile']['name'];
if ($foto_calon){
$config['upload_path'] = './assets/calon/';
$config['allowed_types'] = 'jpg|png|gif|tiff|jpeg';
$this-&gt;load-&gt;library('upload',$config);
if($this-&gt;upload-&gt;do_upload('userfile')){
$userfile = $this-&gt;upload-&gt;data('file_name');
$this-&gt;db-&gt;set('foto_calon', $userfile);
}else{
echo "Gagal Upload";
}
}
$data = array(
'nama_calon' =&gt; $nama_calon,
'parpol_calon' =&gt; $parpol_calon,
'jk_calon' =&gt; $jk_calon,
'lahir_calon' =&gt; $lahir_calon,
'tgl_calon' =&gt; $tgl_calon,
'kerja_calon' =&gt; $kerja_calon,
'dapil_calon' =&gt; $dapil_calon,
'dapil1' =&gt; $dapil1,
'dapil2' =&gt; $dapil2,
'dapil3' =&gt; $dapil3,
'dapil4' =&gt; $dapil4,
'dapil5' =&gt; $dapil5,
'dapil6' =&gt; $dapil6,
'dapil7' =&gt; $dapil7,
'dapil8' =&gt; $dapil8,
'dapil9' =&gt; $dapil9,
'dapil10' =&gt; $dapil10,
'foto_calon' =&gt; $foto_calon,
);
$where = array(
'id' =&gt; $id
);
$this-&gt;calon_model-&gt;update_data($where,$data,'calon');
$this-&gt;session-&gt;set_flashdata('pesan', '&lt;div class="alert alert-danger alert-dismissible fade show" role="alert"&gt;
Profil Calon Berhasil Diubah.
&lt;button type="button" class="close" data-dismiss="alert" aria-label="Close"&gt; &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
&lt;/div&gt;');
redirect('administrator/calon');
}
}</pre><div><br>Semoga berhasil!</div>
<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>
<div>pastikan $id ada di atas if-nya, kaya gini :</div><pre>$id = $this->input->post('id');
if($this->form_validation->run() == FALSE) { $this->update($id); <-- 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>
<div>Wah sudah terpecahkan kak,,terima kasih banyak dn tambahan ternyata form_validation foto harus dihapus juga, yang berikut harus dihapus:</div><pre>$this->form_validation->set_rules('foto_calon','Foto','required',[ 'required' => "Foto Wajib Diisi!" ]);</pre>