When I insert data for the first time the data shows on the table like it should, but when I insert data for the second time my first and second data got deleted.
Here's my tablesupp query (I exported the table, I didn't use query syntax when I made it)
-- phpMyAdmin SQL Dump
-- version 5.1.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 25, 2022 at 03:12 AM
-- Server version: 10.4.21-MariaDB
-- PHP Version: 7.3.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `simstok`
--
-- --------------------------------------------------------
--
-- Table structure for table `tablesupp`
--
CREATE TABLE `tablesupp` (
`idSupp` int(11) NOT NULL,
`namaSupp` varchar(75) NOT NULL,
`alamatSupp` varchar(125) NOT NULL,
`noTelpSupp` varchar(14) NOT NULL,
`Keterangan` varchar(125) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tablesupp`
--
ALTER TABLE `tablesupp`
ADD PRIMARY KEY (`idSupp`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tablesupp`
--
ALTER TABLE `tablesupp`
MODIFY `idSupp` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=32;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
My insert method on Controller (Supplier)
public function save()
{
$this->supplierModel->save([
'namaSupp' => $this->request->getVar('inputNamaSupp'),
'alamatSupp' => $this->request->getVar('inputAlamatSupp'),
'noTelpSupp' => $this->request->getVar('inputNoTelpSupp'),
'Keterangan' => $this->request->getVar('deskripsi')
]);
return redirect()->to(base_url('supplier'));;
}
My Form on view supplier
<form action="/saveSupp" method="POST">
<?= csrf_field(); ?>
<div class="form-group">
<label for="inputNamaUnit">Nama Supplier</label>
<input type="text" class="form-control" name="inputNamaSupp" id="inputNamaSupp" aria-describedby="inputNamaSuppHelp">
</div>
<div class="form-group">
<label for="inputNamaUnit">Alamat Supplier</label>
<input type="text" class="form-control" name="inputAlamatSupp" id="inputAlamatSupp" aria-describedby="inputAlamatSupp">
</div>
<div class="form-group">
<label for="inputNamaUnit">No Telepon</label>
<input type="text" class="form-control" name="inputNoTelpSupp" id="inputNoTelpSupp" aria-describedby="inputNoTelpSuppHelp">
</div>
<div class="form-floating">
<label for="floatingTextarea2">Keterangan</label>
<textarea class="form-control" name="deskripsi" placeholder="Deskripsi (optional)" id="floatingTextarea2" style="height: 100px"></textarea>
</div>
<div class="modal-footer">
<button class="btn btn-light" type="button" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="submit">Save</button>
</div>
</form>
My Model
<?php
namespace App\Models;
use CodeIgniter\Model;
class supplierModel extends Model
{
protected $table = 'tablesupp';
protected $primaryKey = 'idSupp';
protected $allowedFields = ['namaSupp', 'alamatSupp', 'noTelpSupp', 'Keterangan'];
}
Routes
$routes->add('/saveSupp', 'Supplier::save');
I expect the data to not get deleted when I insert it to the table.
I made a mistake on the view file, where I forgot to show you the DELETE Button code. I used http method spoofing that uses tag to achieve it and forgot to close the tag. So the code should be like this :
<form>
<button>DELETE</button>
<form>
INPUT DATA FORM
</form>
When I hit the save button it will automatically execute the delete button because it's in the same tag