Home > Blockchain >  After insert new data, the old data got deleted on MySql CodeIgniter 4
After insert new data, the old data got deleted on MySql CodeIgniter 4

Time:03-31

hello i'm new in stack overflow, codeigniter 4 and dont have enough experience on mysql. I have a problem when i inserted data for the first time the data show on the table like it should be, but when i inserted data for the second time my first data and second got deleted.

here's my tablesupp query (i exported the table, i didnt use query syntax when i made it tho)

-- 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 >
              <label for="inputNamaUnit">Nama Supplier</label>
              <input type="text"  name="inputNamaSupp" id="inputNamaSupp" aria-describedby="inputNamaSuppHelp">
            </div>
            <div >
              <label for="inputNamaUnit">Alamat Supplier</label>
              <input type="text"  name="inputAlamatSupp" id="inputAlamatSupp" aria-describedby="inputAlamatSupp">
            </div>
            <div >
              <label for="inputNamaUnit">No Telepon</label>
              <input type="text"  name="inputNoTelpSupp" id="inputNoTelpSupp" aria-describedby="inputNoTelpSuppHelp">
            </div>
            <div >
              <label for="floatingTextarea2">Keterangan</label>
              <textarea  name="deskripsi" placeholder="Deskripsi (optional)" id="floatingTextarea2" style="height: 100px"></textarea>
            </div>
            <div >
              <button  type="button" data-dismiss="modal">Close</button>
              <button  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');

Thanks in advance for those who want helps me out. :)

I expected my inserted data to the table dont get delete after i insert

CodePudding user response:

use insert() method instate of save() method

in controller :

$myModel = new supplierModel();
$data = [
            'namaSupp' => $this->request->getVar('inputNamaSupp'),
            'alamatSupp' => $this->request->getVar('inputAlamatSupp'),
            'noTelpSupp' => $this->request->getVar('inputNoTelpSupp'),
            'Keterangan' => $this->request->getVar('deskripsi')
        ]
$myModel->insert($data)

and if above method didn't do much add this 3 fields in your database and model ('created_at' ,'updated_at','deleted_at') like blow :

in model

 protected $table      = 'tablesupp';
 protected $primaryKey = 'idSupp';
 protected $allowedFields = ['namaSupp', 'alamatSupp', 'noTelpSupp', 
'Keterangan'];
protected $useAutoIncrement = true;
protected $createdField  = 'created_at';
protected $updatedField  = 'updated_at';
protected $deletedField  = 'deleted_at';

created_at must be curent_timestamp , updated_at and deleted_at can be Text

CodePudding user response:

I have resolved the problem by my own. I'm sorry guys i made a silly mistake on view file I forgot to show you the DELETE Button code, i used http method spoofing that uses tag achieve it then i forgot to close the tag. So the code simply like this :

<form>
<button>DELETE</button>
<form>
INPUT DATA FORM
</form>

the conclusion is, when i hit save button it will automatically execute delete button because it's in the same tag

Thank you guys for those who want help me. (Sorry for my english, its really bad :D)

  • Related