0

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
  • are you sure you get to the $this->db->insert() function? Any error messages? is $data showing any data? How is your database structure? do you have set any index with auto_increment? Commented Mar 3, 2024 at 20:29
  • yes i use function insert() and when I check $data displays any data that has been input but is not inserted to database. i have database structure in my database is tbl_order_ck(id_order_ck : PK auto increment, id_ck : foreign key, nama_order_ck, no_telp_ck, alamat_ck, jenis_paket_ck, waktu_kerja_ck, berat_qty_ck, harga_perkilo, tgl_masuk_ck, tgl_keluar_ck, total_bayar_ck, keterangan_ck. Commented Mar 4, 2024 at 3:51

2 Answers 2

0

You're using the wrong field name in your form validation: the No Telp field is called no_hp in your view, but form validation is checking for a field called no_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:

$this->form_validation->set_rules('no_telp', 'No Telp', 'required', [
        'required' => 'No Telp harus diisi!'
    ]);

To:

$this->form_validation->set_rules('no_hp', 'No Telp', 'required', [
        'required' => 'No Telp harus diisi!'
    ]);
Sign up to request clarification or add additional context in comments.

Comments

0

My Database Structure (tbl_order_ck)

-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 04, 2024 at 04:59 AM
-- Server version: 10.4.28-MariaDB
-- PHP Version: 8.0.28

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: `db_ciproject`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_order_ck`
--

   CREATE TABLE `tbl_order_ck` (
  `id_order_ck` int(11) NOT NULL,
  `id_ck` int(11) NOT NULL,
  `or_ck_number` varchar(10) DEFAULT NULL,
  `nama_order_ck` varchar(100) NOT NULL,
  `no_telp_ck` char(15) NOT NULL,
  `alamat_ck` text NOT NULL,
  `jenis_paket_ck` varchar(100) NOT NULL,
  `waktu_kerja_ck` varchar(30) NOT NULL,
  `berat_qty_ck` int(11) NOT NULL,
  `harga_perkilo` int(11) NOT NULL,
  `tgl_masuk_ck` date NOT NULL,
  `tgl_keluar_ck` date NOT NULL,
  `total_bayar_ck` double NOT NULL,
  `keterangan_ck` text DEFAULT NULL
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

      `--
      -- Indexes for dumped tables
      --`

  --
  -- Indexes for table `tbl_order_ck`
  --
     ALTER TABLE `tbl_order_ck`
     ADD PRIMARY KEY (`id_order_ck`);

  --
  -- AUTO_INCREMENT for dumped tables
  --

  --
  -- AUTO_INCREMENT for table `tbl_order_ck`
  --
     ALTER TABLE `tbl_order_ck`
     MODIFY `id_order_ck` int(11) NOT NULL AUTO_INCREMENT;
     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 */;

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.