Click here to Skip to main content
16,004,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Fatal error: Uncaught mysqli_sql_exception: Cannot add or update a child row: a foreign key constraint fails (`pertandingan`.`urusetia`, CONSTRAINT `urusetia_ibfk_1` FOREIGN KEY (`IDUrusetia`) REFERENCES `peserta` (`IDUrusetia`) ON UPDATE CASCADE) in C:\Users\Acer User\Desktop\XAMPPS\htdocs\pertandingan\urusetia_insert.php:11 Stack trace: #0 C:\Users\Acer User\Desktop\XAMPPS\htdocs\pertandingan\urusetia_insert.php(11): mysqli_query(Object(mysqli), 'insert into uru...') #1 {main} thrown in C:\Users\Acer User\Desktop\XAMPPS\htdocs\pertandingan\urusetia_insert.php on line 11


What I have tried:

<?php
   include("sambungan.php");
   include("urusetia_menu.php");

   if (isset($_POST["submit"])) {
       $idurusetia = $_POST["IDUrusetia"];
       $password = $_POST["PasswordUrusetia"];
       $namaurusetia = $_POST["NamaUrusetia"];
       
       $sql = "insert into urusetia values('$idurusetia', '$password', '$namaurusetia')";
       $result = mysqli_query($sambungan,$sql);
       if ($result == true)
           echo "<br><center>Berjaya tambah</center>";
       else
           echo "<br><center>Ralat : $sql<br>".mysqli_error($sambungan)."</center>";
   }
?>

<link rel="stylesheet"
href="borang.css">
<link rel="stylesheet" href="button.css">
<h3 class="panjang">TAMBAH URUSETIA</h3>
<form class="panjang" action="urusetia_insert.php" method="post">
    <table>
        
        <tr>
            <td>ID Urusetia</td>
            <td><input type="text" name="IDUrusetia"></td>
        </tr>
                
        <tr>
            <td>Nama Urusetia</td>
            <td><input type="text" name="NamaUrusetia"></td>
        </tr>
        
        <tr>
            <td>Password Urusetia</td>
            <td><input type="password" name="PasswordUrusetia" placeholder="max: 8 char"></td>
        </tr>
                
    </table>
    <button class="tambah" type="submit"
    name="submit">Tambah</button>
</form>
Posted
Updated 18-May-22 21:20pm
Comments
Richard MacCutchan 19-May-22 3:15am    
Your update is breaking a foreign key constraint from a linked table. Check your database schema to see why.
Richard Deeming 19-May-22 6:26am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
Richard Deeming 19-May-22 6:26am    
You are also storing passwords in plain text. Don't do that!
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP even has built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]

1 solution

The error is quite clear
Quote:
Cannot add or update a child row: a foreign key constraint fails (`pertandingan`.`urusetia`, CONSTRAINT `urusetia_ibfk_1` FOREIGN KEY (`IDUrusetia`) REFERENCES `peserta` (`IDUrusetia`) ON UPDATE CASCADE)
Your table urusetia column IDUrusetia has a foreign key reference to another table peserta which means that the IDUrusetia must exist on peserta before you can refer to it in urusetia

There is a better explanation here : What is a Foreign Key? (With SQL Examples)[^]

However, given the naming of the column and tables I suspect you have the relationship defined in the wrong direction and that IDUrusetia on peserta should be the foreign key to urusetia, and IDUrusetia on urusetia should be the primary key on that table. See Difference between Primary Key and Foreign Key - GeeksforGeeks[^]
 
Share this answer
 
Comments
HELLO1410 20-May-22 23:21pm    
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900