开发者

CODEIGNITER 4: Error 1452 -Cannot add or update a child row a foreign key constraint fails [closed]

开发者 https://www.devze.com 2022-12-07 20:39 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 1 hour ago.

Improve this question

i have create table


CREATE TABLE admin ( id_admin int(11) NOT NULL, username_admin varchar(225) NOT NULL, nama_admin varchar(225) NOT NULL, password_admin varchar(225) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `admin`
  ADD PRIMARY KEY (`id_admin`);
ALTER TABLE `admin`
  MODIFY `id_admin` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

CREATE TABLE kategori ( id_kategori int(11) NOT NULL, id_adminFK int(11) NOT NULL, nama_kategori varchar(225) NOT NULL, slug_kategori varchar(225) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


ALTER TABLE kategori ADD PRIMARY KEY (id_kategori), ADD KEY id_adminFK (id_adminFK);


ALTER TABLE kategori MODIFY id_kategori int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;


ALTER TABLE kategori ADD CONSTRAINT kategori_ibfk_1 FOREIGN KEY (id_adminFK) REFERENCES admin (id_admin) ON DELETE CASCADE ON UPDATE CASCADE;


CREATE TABLE produk ( id_produk int(11) NOT NULL, id_adminFK int(11) NOT NULL, id_kategoriFK int(11) NOT NULL, nama_produk varchar(225) NOT NULL, slug_produk varchar(225) NOT NULL, harga_produk int(11) NOT NULL, stok int(11) NOT NULL, gambar varchar(225) NOT NULL, deskripsi varchar(225) NOT NULL, size varchar(225) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE produk ADD PRIMARY KEY (id_produk), ADD KEY id_adminFK (id_adminFK), ADD KEY id_kategoriFK (id_kategoriFK);


ALTER TABLE produk MODIFY id_produk int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;


ALTER TABLE produk ADD CONSTRAINT produk_ibfk_1 FOREIGN KEY (id_adminFK) REFERENCES admin (id_admin) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT produk_ibfk_2 FOREIGN KEY (id_kategoriFK) REFERENCES kategori (id_kategori) ON DELETE CASCADE ON UPDATE CASCADE,


this is the code ProdukModel

public function get_listAdmin()
    {
        $data = $this->query('select id_admin, nama_admin from admin');
        return $data->getResultArray();
    }
    public function get_listKategori()
    {    
        $data = $this->query('SELECT id_kategori, nama_kategori 
        FROM kategori');
        return $data->getResultArray();
    }

ProdukController

public function create()
    {
        
        $data = [
            'title' => 'Form Tambah Produk',
            'validation' => \Config\Services::validation(),
            'listAdmin' => $this->produkModel->get_listAdmin(),
            'listKategori' => $this->produkModel->get_listKategori()
        ];
        return view('admin/produk/create', $data);
    }
public function save()
    {
       
        if (!$this->validate([
            
            'id_adminFK' => [
                'rules' => 'required',
                'errors' => [
                    'required' => '{field} harus diisi.'
                ]
            ],
            'id_kategoriFK' => [
                'rules' => 'required',
                'errors' => [
                    'required' => '{field} harus diisi.'
                ]
            ],
            'nama_produk' => [
                'rules' => 'required|is_unique[produk.nama_produk]',
                'errors' => [
                    'required' => '{field} harus diisi.',
                    'is_unique' => '{field} sudah terdaftar.'
                ]
            ],
            'harga_produk' => [
                'rules' => 'required',
                'errors' => [
 开发者_开发知识库                   'required' => '{field} harus diisi.'
                ]
            ],
            'stok' => [
                'rules' => 'required',
                'errors' => [
                    'required' => '{field} harus diisi.'
                ]
            ],
            'gambarProduk' => [
                'rules' => 'uploaded[gambarProduk]|max_size[gambarProduk,10240]|is_image[gambarProduk]|mime_in[gambarProduk,image/jpg,image.jpeg,image/png]',
                
                'errors' => [
                    'uploaded' => 'Pilih Gambar Terlebih dahulu',
                    'max_size' => 'Ukuran Gambar Terlalu Besar',
                    'is_image' => 'Yang Anda Pilih Bukan Gambar',
                    'mime_in' => 'Yang Anda Pilih Bukan Gambar'
                ]
            ],
            'deskripsi' => [
                'rules' => 'required',
                'errors' => [
                    'required' => '{field} harus diisi.'
                ]
            ],
            'size' => [
                'rules' => 'required',
                'errors' => [
                    'required' => '{field} harus diisi.'
                ]
            ],
        ])) {
            
            return redirect()->to(base_url('admin/produk/create'))->withInput();
        }
        

        
        $fileGambar = $this->request->getFile('gambarProduk');

        
        $namaGambar = $fileGambar->getRandomName();
        
        $fileGambar->move('img/produk', $namaGambar);


        
        


        
        //dd($this->request->getVar());
        $slug_produk = url_title($this->request->getVar('nama_produk'), '-', true);
        $this->produkModel->save([
            'nama_produk' => $this->request->getVar('nama_produk'),
            'id_adminFK' => $this->request->getVar('id_adminFK'),
            'id_produkFK' => $this->request->getVar('id_produkFK'),
            'slug_produk' => $slug_produk,
            'harga_produk' => $this->request->getVAr('harga_produk'),
            'stok' => $this->request->getVAr('stok'),
            'gambar' => $namaGambar,
            'deskripsi' => $this->request->getVAr('deskripsi'),
            'size' => $this->request->getVAr('size')
        ]);
        session()->setFlashdata('pesan', 'Data Berhasil ditambahkan');
        return redirect()->to(base_url('/admin/produk'));
    }

View Create

<div class="row mb-3">
                    <label for="id_adminFK" class="col-sm-2 col-form-label">Admin</label>
                    <div class="col-sm-10">
                        <select class="form-select <?= ($validation->hasError('id_adminFK')) ? 'is-invalid' : ''; ?>" name="id_adminFK" id="id_adminFK" aria-label="Default select example">
                            <option selected>Pilih Admin</option>
                            <?php foreach ($listAdmin as $l) {
                                echo '<option value="' . $l['id_admin'] . '">' . $l['nama_admin'] . '</option>';
                            } ?>
                        </select>
                        <div class="invalid-feedback">
                            <?= $validation->getError('id_adminFK'); ?>
                        </div>
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="id_kategoriFK" class="col-sm-2 col-form-label">Kategori Produk</label>
                    <div class="col-sm-10">
                        <select class="form-select <?= ($validation->hasError('id_kategoriFK')) ? 'is-invalid' : ''; ?>" name="id_kategoriFK" id="id_kategoriFK" aria-label="Default select example">
                            <option selected>Pilih Kategori Produk</option>
                            <?php foreach ($listKategori as $lk) {
                                echo '<option value="' . $lk['id_kategori'] . '">' . $lk['nama_kategori'] . '</option>';
                            } ?>
                        </select>
                        <div class="invalid-feedback">
                            <?= $validation->getError('id_kategoriFK'); ?>
                        </div>
                    </di

Any idea what to do? I've been looking for a solution to this problem for a long time.

so I tried to insert data in the produk table in phpmy admin, and it worked.

But when I try to insert data in the produk table table in the codeigniter 4 framework, there is an error 1452 like this the error

I have checked the parent table and the child table are matched. i have done this, but it doesn't work

SET FOREIGN_KEY_CHECKS=0

also I have deleted the database and created a new database and entered data from the beginning and it had no effect. i dont have any idea for this error, so please if you have idea, I've been looking for a solution to this problem for a long time.

0

精彩评论

暂无评论...
验证码 换一张
取 消