简体   繁体   English

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

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

I want to know what the part of the array i'm accessing is. 我想知道我正在访问的数组部分是什么。 I am sending this via $_POST so it would be an array of an array. 我通过$ _POST发送它,所以它将是一个数组的数组。 I have the authors' name and his books. 我有作者的名字和他的书。 I want the be able to get only that one author and only his books with the $_POST in the next.php file and save them in my database. 我希望只能在next.php文件中获得只有一位作者和他的带有$ _POST的书,并将它们保存在我的数据库中。

my tables are: 我的表是:

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>

Add this code to form : 将此代码添加到表单中:

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

next.php 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