无法添加或更新子行,外键约束失败

时间:2015-01-06 02:45:45

标签: php mysql

我正在尝试更新帐户的帐单,但是这个错误就出来了 “无法添加或更新子行:外键约束失败”。我搜索了原因,其中大部分都说是因为插入主表中没有的值。但是,我在帐单注册的表单中,使用包含该主表中的帐号的下拉框,但它仍然有此错误。

表格

<div id="contact_form">


                    <form method="post" action="proses_daftar_bill.php" name="maklumat_bill" oninput= "JumlahBayaran.value = parseFloat(AmaunCaj.value) + parseFloat(AmaunTunggakan.value) + parseFloat(AmaunPenalti.value) + parseFloat(AmaunPelbagai.value) + parseFloat(CajPF.value) + parseFloat(GST.value)">



                        <table>
                        <tr>
                        <label for="NoAkaun">Nombor Akaun</label>
                        <?php
                        require ("dbase.php");
                        $sql    = "SELECT NoAkaun FROM maklumatakaun";
                        $result = mysql_query($sql);
                        echo "<select name='NoAkaun' id='NoAkaun' class='input_field' required /><option></option>";
                        while($kod = mysql_fetch_array($result)){
                        echo "<option value=".$kod['NoAkaun'].">" .$kod['NoAkaun']."</OPTION>";
                        }
                        echo "</select>";

                        ?>  
                        </tr>

                        <tr>
                        <td><label for="BulanBil">Tarikh Bil:</label> </td>
                        <td><select class="required bill_caj" name="BulanBil" id="BulanBil" width="300px" required/>
                        <option value="">--Pilih Bulan--</option>
                        <option value="Januari">Januari</option>
                        <option value="Februari">Februari</option>
                        <option value="Mac">Mac</option>
                        <option value="April">April</option>
                        <option value="Mei">Mei</option>
                        <option value="Jun">Jun</option>
                        <option value="Julai">Julai</option>
                        <option value="Ogos">Ogos</option>
                        <option value="September">September</option>
                        <option value="Oktober">Oktober</option>
                        <option value="November">November</option>
                        <option value="Disember">Disember</option>
                        </select></td>

                        <label for="TahunBil"></label><td><input type="text" maxlength="4" size="4" id="TahunBil" name="TahunBil" class="required year_field" placeholder="Tahun" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required/></td>
                        </tr>

                        <tr>
                        <td>
                        <div class="cleaner_h10"></div>
                        <div class="cleaner_h10"></div>
                        <div class="cleaner_h10"></div>
                        <div class="cleaner_h10"></div>
                        </td>
                        </tr>


                        <tr>    
                        <td><label for="AmaunCaj">Amaun</label></td>
                        <td><input type="text"  name="AmaunCaj" id="AmaunCaj"  class="bill_caj" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required></td>


                        <td><label for="AmaunTunggakan">Tunggakan</label></td>
                        <td><input type="text" name="AmaunTunggakan" id="AmaunTunggakan"  class="bill_caj" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required></td>
                        </tr>

                        <tr>
                        <td><label for="AmaunPenalti">Penalti</label></td>
                        <td><input type="text"   name="AmaunPenalti" id="AmaunPenalti"  class="bill_caj" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"required></td>


                        <td><label for="AmaunPelbagai">Pelbagai</label></td>
                        <td><input type="text" name="AmaunPelbagai" id="AmaunPelbagai"  class="bill_caj" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"required></td>
                        </tr>

                        <tr>
                        <td><label for="GST">GST</label></td>
                        <td><input type="text" name="GST" id="GST"  class="bill_caj" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"required></td>
                        </tr>



                    <tr>
                    <td><label for="checkbox_pf">PF<input type="checkbox" onclick="codename()" name="checkbox_pf" value="ON"></label></td>



                    <td><label for="BacaanPF">Bacaan PF:</label></td>
                    <td><input type="text" id="BacaanPF" name="BacaanPF" class="bill_caj" value="0" disabled onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"></td>
                    </tr>

                    <tr>
                    <td></td>
                    <td><label for="CajPF">Caj PF:</label></td>
                    <td><input type="text" id="CajPF" name="CajPF" class="bill_caj" value="0" disabled onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"></td>
                    </tr>




                        <tr>
                        <td><label for="JumlahBayaran">Jumlah Bayaran:</label></td>
                        <td><input id="JumlahBayaran" name="JumlahBayaran" class="bill_caj" for="AmaunCaj AmaunTunggakan" step="any" readonly></td>
                        <div class="cleaner_h10"></div>
                        </tr>






                        <tr>
                        <td><input type="submit" value="Daftar" id="submit" name="register-submit" class="submit_btn " /></td>
                        <td><input type="reset" value="Batal" id="reset" name="reset" class="submit_btn" /></td>
                        </tr>

                        </table>

                    </form>

sql

<?php
require("dbase.php"); 

if ($_POST) {

    $TarikhData         = isset($_POST['TarikhData'])       ? $_POST['TarikhData'] : '';
    $username           = isset($_POST['username'])         ? $_POST['username'] : '';
    $NoAkaun            = isset($_POST['NoAkaun'])          ? $_POST['NoAkaun'] : '';
    $BulanBil           = isset($_POST['BulanBil'])         ? $_POST['BulanBil'] : '';
    $TahunBil           = isset($_POST['TahunBil'])         ? $_POST['TahunBil'] : '';
    $AmaunCaj           = isset($_POST['AmaunCaj'])         ? $_POST['AmaunCaj'] : '';
    $AmaunTunggakan     = isset($_POST['AmaunTunggakan'])   ? $_POST['AmaunTunggakan'] : '';
    $AmaunPenalti       = isset($_POST['AmaunPenalti'])     ? $_POST['AmaunPenalti'] : '';
    $AmaunPelbagai      = isset($_POST['AmaunPelbagai'])    ? $_POST['AmaunPelbagai'] : '';
    $GST                = isset($_POST['GST'])              ? $_POST['GST'] : '';
    $BacaanPF           = isset($_POST['BacaanPF'])         ? $_POST['BacaanPF'] : '';
    $CajPF              = isset($_POST['CajPF'])            ? $_POST['CajPF'] : '';
    $JumlahBayaran      = isset($_POST['JumlahBayaran'])    ? $_POST['JumlahBayaran'] : '';




    $sql = mysql_query("INSERT INTO maklumatbilakaun VALUES ('', '$TarikhData' , '$username' , '$NoAkaun' , '$BulanBil' , '$TahunBil' ,'$AmaunCaj' ,'$AmaunTunggakan' ,'$AmaunPenalti' ,'$AmaunPelbagai' , '$GST' , '$BacaanPF' ,'$CajPF' ,'$JumlahBayaran')");
    echo mysql_error();
}






?>

究竟是什么问题?

1 个答案:

答案 0 :(得分:0)

如果没有在insert语句中为值指定列,则很可能会将值插入错误的列中。

$sql = mysql_query("INSERT INTO 
maklumatbilakaun (TarikhData, username, NoAkaun, BulanBil, TahunBil, AmaunCaj, AmaunTunggakan, AmaunPenalti, AmaunPelbagai, GST, BacaanPF, CajPF, JumlahBayaran)
VALUES ('$TarikhData' , '$username' , '$NoAkaun' , '$BulanBil' , '$TahunBil' ,'$AmaunCaj' ,'$AmaunTunggakan' ,'$AmaunPenalti' ,'$AmaunPelbagai' , '$GST' , '$BacaanPF' ,'$CajPF' ,'$JumlahBayaran')");

你还需要在列中提供DEFAULT而不是空值,或者在构建insert语句时省略列(我更喜欢后一种方法,虽然我不确定php中字符串连接的效率如何)。