help me… I want to create a laundry program and i use codeigniter 3. when I programmed on the user side, I experienced the problem of data not inserted to database. even though the database name, table name, and column name are correct. from the database to field name are correct. i try to add handling error is the same, this is a bug something. give the code to fixed it please.
View
`<!– Begin Page Content –>
<!-- Page Heading -->
<div class="row">
<!-- Page Heading -->
<div class="col-sm-1">
<a href="<?= base_url('home/index'); ?>" class="btn btn-outline-primary"><i class="fas fa-arrow-left"></i></a>
</div>
<div class="col-sm-5">
<h1 class="h3 mb-4 text-gray-800"><?= $title; ?></h1>
</div>
</div>
<div class="container">
<form name="frm_add" action="<?= base_url('home/tambahOrderCk'); ?>" method="post">
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput1">Nama</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Masukan Nama" value="<?= $user['name'] ?>">
<?= form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">No Handphone Aktif</label>
<input type="number" class="form-control" id="no_hp" name="no_hp" placeholder="Masukan Nomor" value="<?= $user['phone_number'] ?>">
<?= form_error('no_hp', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Jenis Paket</label>
<select class="form-control" id="jenis_paket" name="jenis_paket">
<?php foreach ($jenis_paket as $jp) : ?>
<option value="<?= $jp['id_ck'] ?>"><?= $jp['nama_paket_ck'] ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="keterangan">Catatan</label>
<textarea class="form-control" id="keterangan" name="keterangan" rows="3"></textarea>
</div>
</div>
<div class="col">
<div class="form-group">
<label for="address">Alamat</label>
<textarea class="form-control" rows="2" id="address" name="address"><?= $user['address'] ?></textarea>
<?= form_error('address', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="form-group">
<label for="berat_cucian">Berat Cucian (/Kg)</label>
<input type="number" class="form-control" id="berat_cucian" name="berat_cucian" placeholder="Masukan Berat Cucian">
<?= form_error('berat_cucian', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
<button type="submit" class="btn btn-primary">
Pesan Sekarang
</button>
</form>
</div>
`
Controller
`public function tambahOrderCk()
{
$this->load->library('form_validation');
$data['title'] = 'Tambah Order Cuci Komplit';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['jenis_paket'] = $this->db->get('tbl_cuci_komplit')->result_array();
// echo "Nilai $nama_order: " . $nama_order;
// echo "Nilai $alamat: " . $alamat;
// echo "Nilai $jenis_paket: " . $jenis_paket;
// echo "Nilai $berat: " . $berat;
// echo "Nilai $waktuKerja: " . $waktuKerja;
// echo "Nilai $tarif_ck: " . $tarif_ck;
$this->form_validation->set_rules('name', 'Name', 'required', [
'required' => 'Name harus diisi!'
]);
$this->form_validation->set_rules('no_telp', 'No Telp', 'required', [
'required' => 'No Telp harus diisi!'
]);
$this->form_validation->set_rules('address', 'Address', 'required', [
'required' => 'Alamat harus diisi!'
]);
$this->form_validation->set_rules('berat_cucian', 'Berat Cucian', 'required', [
'required' => 'Berat Cucian harus diisi!'
]);
$or_ck_number = 'CK/' . date('Ymd') . '/' . rand(100000, 999999);
$nama_order = htmlspecialchars($this->input->post('name', true));
$alamat = htmlspecialchars($this->input->post('address'), true);
$jenis_paket = htmlspecialchars($this->input->post('jenis_paket'), true);
$berat = htmlspecialchars($this->input->post('berat_cucian'), true);
$waktuKerja = 0;
$tarif_ck = 0;
if ($jenis_paket == 1 or $jenis_paket == 2 or $jenis_paket == 3) {
$waktu_Kerja = $this->db->get_where('tbl_cuci_komplit', ['id_ck' => $jenis_paket])->row_array();
$waktuKerja = (string)$waktu_Kerja['waktu_kerja_ck'];
$tarifCk = $this->db->get_where('tbl_cuci_komplit', ['id_ck' => $jenis_paket])->row_array();
$tarif_ck = $tarifCk['tarif_ck'];
}
if ($this->form_validation->run() == true) {
$data = [
'or_ck_number' => $or_ck_number,
'nama_order_ck' => $nama_order,
'no_telp_ck' => htmlspecialchars($this->input->post('no_telp'), true),
'alamat_ck' => $alamat,
'jenis_paket_ck' => $jenis_paket,
'waktu_kerja_ck' => $waktuKerja,
'berat_qty_ck' => $berat,
'harga_perkilo' => $tarif_ck,
'tgl_masuk_ck' => date('Y-m-d'),
'tgl_keluar_ck' => date('Y-m-d', strtotime('+' . $waktuKerja . 'days')),
'total_bayar_ck' => $tarif_ck * htmlspecialchars($this->input->post('berat_cucian'), true),
'keterangan_ck' => htmlspecialchars($this->input->post('keterangan'), true)
];
$this->db->insert('tbl_order_ck', $data);
// var_dump($this->db->affected_rows());
// die;
try {
if ($this->db->affected_rows() > 0) {
echo "Data berhasil dimasukkan ke dalam database";
} else {
echo "Error: " . $this->db->error();
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
// if ($this->db->affected_rows() > 0) {
// echo "Data berhasil dimasukkan ke dalam database";
// } else {
// echo ""
// }
redirect('home/tambahOrderCk');
} else {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('home/tambahOrderCk', $data);
$this->load->view('templates/footer');
}
}
}`
2
Answers
My Database Structure (tbl_order_ck)
You’re using the wrong field name in your form validation: the
No Telp
field is calledno_hp
in your view, but form validation is checking for a field calledno_telp
. Since that field is required but never exists when you submit the form, form validation fails and your data is not inserted.Change this:
To: