如何将PHP中的数组数组插入mysql数据库

时间:2015-12-09 07:55:57

标签: php jquery arrays

我想知道我正在访问的数组部分是什么。我通过$ _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>

1 个答案:

答案 0 :(得分:0)

将此代码添加到表单:

<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 ...
    }
}