将数据插入到两个表中,从而导致"数组到字符串转换"

时间:2015-01-02 02:11:52

标签: javascript php html mysql

我尝试为我的系统创建表单,用户可以添加输入字段的数量,输入字段大多是下拉框,其中的选项来自数据库中的表。表单会将数据插入两个不同的数据库。但它显示"数组到字符串转换的错误"现在数据只插入到第一个表中。这是我到目前为止所做的事情

我的表单代码:

<form method="post" name="maklumat_akaun" action="proses_daftar_akaun.php">



                    <label for="NoAkaun">No. Akaun</label> 
                    <input type="text" id="NoAkaun" name="NoAkaun" class="required input_field" required/> 
                    <div class="cleaner_h10"></div> 




                    <label for="KodDaerah">Daerah</label>
                    <?php
                    include('dbase.php');$sql   = "SELECT KodDaerah, NamaDaerah FROM koddaerah";
                    $result = mysql_query($sql);
                    echo "<select name='KodDaerah' id='KodDaerah' class='input_field' required /><option></option>";
                    while($kod = mysql_fetch_array($result)){
                    echo "<option value=".$kod['KodDaerah'].">" .$kod['NamaDaerah']."</OPTION>";
                    }
                    echo "</select>";
                    ?>  
                    <div class="cleaner_h10"></div> 



                    <label for="KodBahagian">Bahagian</label>
                    <?php
                    $sql    = "SELECT KodBahagian, NamaBahagian FROM kodbahagian";
                    $result = mysql_query($sql);
                    echo "<select name='KodBahagian' id='KodBahagian' class='input_field' required /><option></option>";
                    while($kod = mysql_fetch_array($result)){
                    echo "<option value=".$kod['KodBahagian'].">" .$kod['NamaBahagian']."</OPTION>";
                    }
                    echo "</select>";
                    ?>  
                    <div class="cleaner_h10"></div>


                    <label for="KodKategori">Kategori Akaun</label>                        
                    <?php
                    $sql    = "SELECT KodKategori, NamaKategori , SubKategori FROM kodkategori";
                    $result = mysql_query($sql);
                    echo "<select name='KodKategori' id='KodKategori' class='input_field' required /><option></option>";
                    while($kod = mysql_fetch_array($result)){
                    echo "<option value=".$kod['KodKategori'].">" .$kod['NamaKategori']." (".$kod['SubKategori'].")</OPTION>";
                    }
                    echo "</select>";
                    ?>                  
                    <div class="cleaner_h10"></div>








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







<!-----------------------------------------------------------//-->  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div>'+
                                '<td> <?php
                                $sql  = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
                                $result = mysql_query($sql);
                                echo "<select name=\'KodLokasi[]\' id=\'KodLokasi[]\' class=\'input_field\' required ><option></option>";
                                while($kod = mysql_fetch_array($result)){
                                echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']. "</OPTION>";
                                }
                                echo "</select>";
                                ?> </td> </tr>'+

                                '<tr> <td> <?php
                                $sql  = "SELECT KodJenisAkaun, NamaJenisAkaun FROM kodjenisakaun";
                                $result = mysql_query($sql);
                                echo "<select name=\'KodJenisAkaun[]\' id=\'KodJenisAkaun[]\' class=\'input_field\' required ><option></option>";
                                while($kod = mysql_fetch_array($result)){
                                echo "<option value=".$kod['KodJenisAkaun'].">" .$kod['NamaJenisAkaun']. "</OPTION>";
                                }
                                echo "</select>";
                                ?> </td>'+


                                '<td> <input type="text" name="NoTelefon[]" id="NoTelefon[]" value="0" class="required input_field"> </td>' +

                                '<a href="#" class="remove_field">Batal</a></tr></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    });
});

</script>


                        <fieldset>

                        <div class="input_fields_wrap">
                        <h3 class="add_field_button"><a href="">Add More Fields</a></h3>  


        <table>
        <tr>
            <td> <label for="KodLokasi">Lokasi</label> </td> <td> <label for="KodJenisAkaun">Jenis Akaun</label> </td> <td> <label>No.Telefon:</label> </td>

        </tr>

        <tr>

            <td>    <?php 
                    $sql    = "SELECT KodLokasi, NamaLokasi FROM kodlokasi";
                    $result = mysql_query($sql);
                    echo "<select name='KodLokasi[]' id='KodLokasi' class='input_field' required /><option></option>";
                    while($kod = mysql_fetch_array($result)){
                    echo "<option value=".$kod['KodLokasi'].">" .$kod['NamaLokasi']."</OPTION>";
                    }
                    echo "</select>";
                    ?>
            </td>


            <td>    <?php
                    $sql    = "SELECT KodJenisAkaun, NamaJenisAkaun FROM kodjenisakaun";
                    $result = mysql_query($sql);
                    echo "<select name='KodJenisAkaun[]' id='KodJenisAkaun' class='input_field' required /><option></option>";
                    while($kod = mysql_fetch_array($result)){
                    echo "<option value=".$kod['KodJenisAkaun'].">" .$kod['NamaJenisAkaun']."</OPTION>";
                    }
                    echo "</select>";
                    ?>
            </td>       

            <td> <input type="text" name="no_telefon[]" value="0" class="required input_field" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"> </td>                    

        </tr>

        </table>


                        </div>




                        </fieldset>
<!-----------------------------------------------------------//-->                          

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


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


                    </table>
                    </form>

虽然这是我插入过程的代码。

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

if ($_POST) {

    $NoAkaun            = isset($_POST['NoAkaun'])          ? $_POST['NoAkaun'] : '';
    $KodBahagian        = isset($_POST['KodBahagian'])      ? $_POST['KodBahagian'] : '';
    $Tarif              = ISSET($_POST['Tarif'])            ? $_POST['Tarif'] : '';
    $KodDaerah          = isset($_POST['KodDaerah'])        ? $_POST['KodDaerah'] : '';
    $KodKategori        = isset($_POST['KodKategori'])      ? $_POST['KodKategori'] : '';
    $NoTelefon          = isset($_POST['NoTelefon'])        ? $_POST['NoTelefon'] : '';
    $KodLokasi          = isset($_POST['KodLokasi'])        ? $_POST['KodLokasi'] : '';
    $KodJenisAkaun      = isset($_POST['KodJenisAkaun'])    ? $_POST['KodJenisAkaun'] : '';
    $akaun_idAkaun      = isset($_POST['akaun_idAkaun'])    ? $_POST['akaun_idAkaun'] : '';




    $sql = mysql_query("INSERT INTO maklumatakaun VALUES ('', '$NoAkaun'  , '$KodBahagian' , '$KodDaerah' , '$KodKategori' ,  '$Tarif' )");
    $akaun_idAkaun = mysql_insert_id();
    foreach ($NoTelefon AS $i => $telefon) {
    $sql = mysql_query("INSERT INTO detailakaun VALUES   ('',  '$KodLokasi[$i]', '$KodJenisAkaun' , '$telefon' , '$akaun_idAkaun' )");
}



    echo "<script type='text/javascript'> alert('AKAUN BERJAYA DIDAFTARKAN')</script> ";
    echo "<script type='text/javascript'>window.location='pilih_kategori_daftar.php'</script>";
}


?>

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

错误“数组到字符串转换”意味着您在代码中的某个位置使用数组作为字符串。该错误消息后面通常是文件名和行号,这可以帮助您缩小搜索范围。查看变量中包含的内容的一种有用方法是使用以下内容:

echo '<pre>'; print_r($stuff); die();

如果错误发生在while循环内的一行上,你应该在while和die()之前放置echo'';之后,您可以在循环中查看问题的所有实例。

答案 1 :(得分:0)

我在这个陈述中发现了你的问题

$sql = mysql_query("INSERT INTO detailakaun VALUES   ('',  '$KodLokasi[$i]', '$KodJenisAkaun' , '$telefon' , '$akaun_idAkaun' )");

$KodJenisAkaun是一个数组,您可以将其用作字符串