1

I get problem while inserting data to database. It get an error like this:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (sipp-litbang1.atribut_proposal, CONSTRAINT fkAtributProposal FOREIGN KEY (id_pengguna) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE)array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL }

I make method to insert data, like this:

      public function pengajuan_proposal($pengguna, $judul_pro, $abstrak, $keywords, $lama_kegiatan, $bidang_fokus_id, $bidang_fokus_at, $topik_penelitian,
                                           $nidn, $nama_personil, $prodi, $jen_pend, $jab_fung, $danus1, $danus2, $danus3, $danin1, $danin2,
                                           $danin3){

            $this->conn->beginTransaction();
            try {


                $query = $this->conn->prepare("INSERT INTO identitas_proposal 
                                              (id_pengguna, judul_pro, abstrak, keywords, lama_kegiatan, bidang_fokus)
                                              VALUES (:id_pengguna, :judulpro, :abstrak, :keywords, :lamakegiatan, :bidangfokus)");
                $query->bindParam(':id_pengguna', $pengguna, PDO::PARAM_STR);
                $query->bindParam(':judulpro', $judul_pro, PDO::PARAM_STR);
                $query->bindParam(':abstrak', $abstrak, PDO::PARAM_STR);
                $query->bindParam(':keywords', $keywords, PDO::PARAM_STR);
                $query->bindParam(':lamakegiatan', $lama_kegiatan, PDO::PARAM_STR);
                $query->bindParam(':bidangfokus', $bidang_fokus_id, PDO::PARAM_STR);
                $query->execute();

                $lastInsertID = $this->conn->lastInsertId();

                $query = $this->conn->prepare("INSERT INTO atribut_proposal 
                                              (id_pengguna, bidang_fokusatribut, topik_penelitian)
                                              VALUES (:id_pengguna, :bidangfokusatribut, :topikpenelitian)");
                $query->bindParam(':id_pengguna', $lastInsertID, PDO::PARAM_STR);
                $query->bindParam(':bidangfokusatribut', $bidang_fokus_at, PDO::PARAM_STR);
                $query->bindParam(':topikpenelitian', $topik_penelitian, PDO::PARAM_STR);
                $query->execute();

                $query = $this->conn->prepare("INSERT INTO daftar_personil 
                                              (id_pengguna, nidn, nama_personil, program_studi, jenjang_pend, jabatan)
                                              VALUES (:id_pengguna, :nidn, :namapersonil, :programstudi, :jenjangpendidikan, :jabatan)");
                $query->bindParam(':id_pengguna', $lastInsertID, PDO::PARAM_STR);
                $query->bindParam(':nidn', $nidn, PDO::PARAM_STR);
                $query->bindParam(':namapersonil', $nama_personil, PDO::PARAM_STR);
                $query->bindParam(':programstudi', $prodi, PDO::PARAM_STR);
                $query->bindParam(':jenjangpendidikan', $jen_pend, PDO::PARAM_STR);
                $query->bindParam(':jabatan', $jab_fung, PDO::PARAM_STR);
                $query->execute();

                $query = $this->conn->prepare("INSERT INTO biaya_usulan 
                                              (id_pengguna, biaya_tahun1, biaya_tahun2, biaya_tahun3)
                                              VALUES (:id_pengguna, :biayatahun1, :biayatahun2, biayatahun3)");
                $query->bindParam(':id_pengguna', $lastInsertID, PDO::PARAM_STR);
                $query->bindParam(':biayatahun1', $danus1, PDO::PARAM_STR);
                $query->bindParam(':biayatahun2', $danus2, PDO::PARAM_STR);
                $query->bindParam(':biayatahun3', $danus3, PDO::PARAM_STR);
                $query->execute();

                $query = $this->conn->prepare("INSERT INTO biaya_internal 
                                              (id_pengguna, biaya_tahun1in, biaya_tahun2in, biaya_tahun3in)
                                              VALUES (:id_pengguna, :biayatahun1in, :biayatahun2in, biayatahun3in)");
                $query->bindParam(':id_pengguna', $lastInsertID, PDO::PARAM_STR);
                $query->bindParam(':biayatahun1in', $danin1, PDO::PARAM_STR);
                $query->bindParam(':biayatahun2in', $danin2, PDO::PARAM_STR);
                $query->bindParam(':biayatahun3in', $danin3, PDO::PARAM_STR);
                $query->execute();

                $this->conn->commit();
            }
            catch (PDOException $e){
                $this->conn->rollBack();
                echo $e->getMessage();
                var_dump($this->conn->errorInfo());
            }

            return true;
        }
    }

Then, on pengajuan.php file, I write codes like this:

<?php

    require_once __DIR__ . "/../class.proposal.php";

    $proposal = new PROPOSAL();
    if (isset($_POST['submit'])){
        $pengguna           = $_SESSION['userSession'];
        $judulpro           = $_POST['judulproposal'];
        $abstrak            = $_POST['abstrak'];
        $keywords           = $_POST['keywords'];
        $lama_kegiatan      = $_POST['lamakegiatan'];
        $bidang_fokus_id    = $_POST['bidangfokusid'];
        $bidang_fokus_at    = $_POST['bidangfokusat'];
        $topik_penelitian   = $_POST['topik'];
        $nidn               = $_POST['nidn'];
        $nama_personil      = $_POST['namapersonil'];
        $program_studi      = $_POST['programstudi'];
        $jenjang_pendidikan = $_POST['jenjangpendidikan'];
        $jabatan            = $_POST['jabatan'];
        $biaya_tahun1us     = $_POST['danus1'];
        $biaya_tahun2us     = $_POST['danus2'];
        $biaya_tahun3us     = $_POST['danus3'];
        $biaya_tahun1in     = $_POST['danin1'];
        $biaya_tahun2in     = $_POST['danin2'];
        $biaya_tahun3in     = $_POST['danin3'];


        try {
            if($proposal->pengajuan_proposal($pengguna, $judulpro, $abstrak, $keywords, $lama_kegiatan, $bidang_fokus_id, $bidang_fokus_at,
                $topik_penelitian, $nidn, $nama_personil, $program_studi, $jenjang_pendidikan,
                $jabatan, $biaya_tahun1us, $biaya_tahun2us, $biaya_tahun3us, $biaya_tahun1in,
                $biaya_tahun2in, $biaya_tahun3in)){


            }
        }
        catch (PDOException $e){
            echo $e->getMessage();
        }
    }
?>

I have tried to find solutions here and have implemented like delete parent tables (user, in my case), and create that table again or change default field id_pengguna into null, but it didn't work. What's the problem? Or maybe my codes still have errors or something else. Sorry for my broken english, by the way.

beginnerrr
  • 35
  • 1
  • 9
  • Possible duplicate off https://stackoverflow.com/questions/17648179/sqlstate23000-integrity-constraint-violation-1452-cannot-add-or-update-a-chi?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – gaurav Jun 03 '18 at 07:50

1 Answers1

1

@VTaehyung, you are facing issue due to foreign key constraints. either "remove foreign key constraint" or ensure id_pengguna column value should be present in user table in id column.

we can't add a value in foreign table unless it present in primary table. for example , we can't add a employee in a department ,which is not exist yet.

For example, the Sales.SalesOrderHeader table has a foreign key link to the Sales.SalesPerson table because there is a logical relationship between sales orders and salespeople. The SalesPersonID column in the SalesOrderHeader table matches the primary key column of the SalesPerson table. The SalesPersonID column in the SalesOrderHeader table is the foreign key to the SalesPerson table. By creating this foreign key relationship, a value for SalesPersonID cannot be inserted into the SalesOrderHeader table if it does not already exist in the SalesPerson table.

references : https://learn.microsoft.com/en-us/sql/relational-databases/tables/primary-and-foreign-key-constraints?view=sql-server-2017

nagendra patod
  • 289
  • 2
  • 8
  • Okay thanks for your explanation. Actually I have had value in id field(pk) of user table , when I echo it on pengajuan.php it has value which is the value was 1. As you said, 'we can't add a value in foreign table unless it present in primary table', but my primary already has value. I wonder what's going on? You can see from my codes, I set id_pengguna as $_SESSION['userSession'] so I insert id_pengguna based on who is loggedin. – beginnerrr Jun 02 '18 at 17:52
  • I Think , foreign key constraints fails on table atribut_proposal , in which id_pengguna value equal to last inserted id in identitas_proposal. can you change << $query->bindParam(':id_pengguna', $lastInsertID, PDO::PARAM_STR);>> to $query->bindParam(':id_pengguna', $pengguna, PDO::PARAM_STR); – nagendra patod Jun 02 '18 at 18:09
  • Thanks for your suggestion, It had worked. lastinsertID() function can't be used for this case. – beginnerrr Jun 04 '18 at 08:16