簡體   English   中英

如何在PHP中將數組的數組插入mysql數據庫

[英]How do I insert an array of an array in PHP into a mysql database

我想知道我正在訪問的數組部分是什么。 我通過$ _POST發送它,所以它將是一個數組的數組。 我有作者的名字和他的書。 我希望只能在next.php文件中獲得只有一位作者和他的帶有$ _POST的書,並將它們保存在我的數據庫中。

我的表是:

AUTHOR
idAuthor //primary key
nameAuthor

BOOK
idBook //primary key
nameAuthor
bookName 

/

<!--        http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery-->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
   <form action="next.php" method="post">
        <div class="input_fields_wrap">
            <button class="add_field_button">Add Author</button>

            <div id="commonPart" class="commonPart">
                <br>
            <div><input type="text" name="myAuthorText[]" placeholder="Auth name"></div>

            <button class="add_sub_field_button">Add Author Books</button>
            <div><input type="text" class="bookname" name="myBooksText[]" placeholder="Book name"></div>
             </div> 
        </div>
    </form> 

<!--        http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery-->

    </form>    

</div> 

<SCRIPT language="text/javascript">

    //http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery


$(document).ready(function() {
    var max_fields      = 20; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var commonPart      = $("#commonPart"); 
    var add_button      = $(".add_field_button"); //Add button ID
    var add_subButton      = $(".add_sub_field_button"); //Add sub button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            var htmlToAdd = commonPart.clone().attr("id","commonPart_"+x);
            htmlToAdd.find(".addedDiv").remove();
            $(wrapper).append(htmlToAdd); //add input box
          x++; //text box increment
        }
    });

    $(document).on("click",".add_sub_field_button",function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(this).closest(".commonPart").append('<div class="addedDiv"><input type="text" class="bookname" name="myBooksText[]" placeholder="Book name"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

//http://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery
</SCRIPT>

將此代碼添加到表單中:

<input name="submit" type="submit" value="Go"/>

next.php

if($_POST['submit']){
    $myAuthorText = $_POST['myAuthorText'];
    foreach($myAuthorText as $value){
        echo $value; // Value of each author
        // AUTHOR
        // INSERT ...
    }

    $myBooksText = $_POST['myBooksText'];
    foreach($myBooksText as $value){
        echo $value; // Value of each text
        // TEXT
        // INSERT ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM