I have my input inside my form, and I could get all the data into the controller.
My Form inside the view
<form action="/arsip/save" method="post">
<?= csrf_field(); ?>
<div class="mb-3">
<label for="nama_pemegang_hak" class="form-label">Nama Pemegang Hak</label>
<input type="text" name="nama_pemegang_hak" class="form-control <?= $validation->hasError('nama_pemegang_hak') ? 'is-invalid' : ''; ?>" id=" nama_pemegang_hak" value="<?= old('nama_pemegang_hak'); ?>">
</div>
<div class="mb-3">
<label class="form-label" for="nama_hak">Nama Hak</label>
<select class="form-select" id="nama_hak" name="nama_hak">
<option value="Hak Milik">Hak Milik</option>
<option value="Hak Guna Bangunan">Hak Guna Bangunan</option>
<option value="Warkah">Warkah</option>
</select>
</div>
<div class="mb-3">
<label for="nomor_hak" class="form-label">Nomor Hak</label>
<input type="text" name="nomor_hak" class="form-control <?= $validation->hasError('nomor_hak') ? 'is-invalid' : ''; ?>" id="nomor_hak" value="<?= old('nomor_hak'); ?>">
</div>
<div class="mb-3">
<label for="kelurahan" class="form-label">Kelurahan</label>
<input type="text" name="kelurahan" class="form-control <?= $validation->hasError('kelurahan') ? 'is-invalid' : ''; ?>" id="kelurahan" value="<?= old('kelurahan'); ?>">
</div>
<div class="mb-3">
<label for="kecamatan" class="form-label">Kecamatan</label>
<input type="text" name="kecamatan" class="form-control <?= $validation->hasError('kecamatan') ? 'is-invalid' : ''; ?>" id="kecamatan" value="<?= old('kecamatan'); ?>">
</div>
<div class="mb-3">
<label for="no_lemari" class="form-label">Nomor Lemari</label>
<input type="number" name="no_lemari" min="1" class="form-control <?= $validation->hasError('no_lemari') ? 'is-invalid' : ''; ?>" id="no_lemari" value="<?= old('no_lemari'); ?>">
</div>
<div class="mb-3">
<label for="no_rak" class="form-label">Nomor Rak</label>
<input type="number" name="no_rak" min="1" class="form-control <?= $validation->hasError('no_rak') ? 'is-invalid' : ''; ?>" id="no_rak" value="<?= old('no_rak'); ?>">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
All the data is going to be collected inside my controller. After it went pass the validation (it's just required and is_unique for the validation), the data is going to be saved inside the database. Everything went well except that the data from no_lemari and no_rak is not saved into the database (in the database, it only put the number 0 even though my input is not 0).
Controller code to save into database
$this->bukuModel->save([
'nama_pemegang_hak' => $this->request->getVar('nama_pemegang_hak'),
'nama_hak' => $this->request->getVar('nama_hak'),
'nomor_hak' => $this->request->getVar('nomor_hak'),
'kelurahan' => $this->request->getVar('kelurahan'),
'kecamatan' => $this->request->getVar('kecamatan'),
'no_lemari' => intval($this->request->getVar('no_lemari')),
'no_rak' => intval($this->request->getVar('no_rak'))
]);
session()->setFlashdata('pesan', 'Data berhasil ditambahkan.');
return redirect()->to('/arsip/home');
I used intval to change the data type into int (it works when I use dd to check it), but the value inside the database is still 0. I tried doing this
$nomor_lemari = $this->request->getVar('no_lemari');
$nomor_rak = intval($this->request->getVar('no_rak'));
and changing the code in the save into this
'no_lemari' => $nomor_lemari,
'no_rak' => $nomor_rak
but nothing really changes as well. Can anyone tell me what is wrong and what do I need to change?
My table structure
Since I can't find the the reason why the data keep being 0, I ended up removing the CI save method and saved my data in this way
$db = \Config\Database::connect();
$nama_pemegang_hak = $this->request->getVar('nama_pemegang_hak');
$nama_hak = $this->request->getVar('nama_hak');
$nomor_hak = $this->request->getVar('nomor_hak');
$kelurahan = $this->request->getVar('kelurahan');
$kecamatan = $this->request->getVar('kecamatan');
$no_lemari = (int) $this->request->getVar('no_lemari');
$no_rak = (int) $this->request->getVar('no_rak');
// Execute INSERT SQL query
$db->query(
"INSERT INTO buku (nama_pemegang_hak, nama_hak, nomor_hak, kelurahan, kecamatan, no_lemari, no_rak) VALUES (?, ?, ?, ?, ?, ?, ?)",
[$nama_pemegang_hak, $nama_hak, $nomor_hak, $kelurahan, $kecamatan, $no_lemari, $no_rak]
);
For my update, I just added this line into my code
$db = \Config\Database::connect();
$db->query("UPDATE buku SET no_lemari = ?, no_rak = ? WHERE id = ?", [$no_lemari, $no_rak, $id]);
So the entire code turn into this
$no_lemari = (int)$this->request->getVar('no_lemari');
$no_rak = (int)$this->request->getVar('no_rak');
$this->bukuModel->save([
'id' => $id,
'nama_pemegang_hak' => $this->request->getVar('nama_pemegang_hak'),
'nama_hak' => $this->request->getVar('nama_hak'),
'nomor_hak' => $this->request->getVar('nomor_hak'),
'kelurahan' => $this->request->getVar('kelurahan'),
'kecamatan' => $this->request->getVar('kecamatan'),
'no_lemari' => $no_lemari,
'no_rak' => $no_rak
]);
$db = \Config\Database::connect();
$db->query("UPDATE buku SET no_lemari = ?, no_rak = ? WHERE id = ?", [$no_lemari, $no_rak, $id]);
Well, so I basically just brute force the data save using the query. I still don't really understand the problem but forcefully updating the data worked so I'll just use this until anyone gave me a better answer