skip to Main Content

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 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 AppModels;

use CodeIgniterModel;

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

2

Answers


  1. Chosen as BEST ANSWER

    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)


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search