How to add values of generated textbox in database using mysqli
<p>This code somehow stores only the value of last textbox. When we generate 3 textboxes, only the value of 3rd is stored in database instead of all. I want to store <strong>all</strong> the generated textbox values in the database</p>

<pre><code> &lt;?php if(isset($_POST['submit'])){
$num_cat = $_POST['num_cat'];
$text = $_POST['category'];
foreach ($text as $key) {
  // echo $key."\n";
}
$insertquery = mysqli_query($con, "INSERT INTO accounts (accountusername, accountemail) VALUES('".$num_cat."', '".$key."')");
if(!$insertquery){
  echo "Error".mysqli_error($con);
}
} ?&gt;

&lt;script type="text/javascript"&gt;
    //when the webpage has loaded do this
    $(document).ready(function() {  
        //if the value within the dropdown box has changed then run this code            
        $('#num_cat').change(function(){
            //get the number of fields required from the dropdown box
            var num = $('#num_cat').val();                  

            var i = 0; //integer variable for 'for' loop
            var textboxes = ''; //string variable for html code for fields 
            //loop through to add the number of fields specified
            for (i=1;i&lt;=num;i++) {
                //concatinate number of fields to a variable
                textboxes += 'Category'+i+': &lt;input type="text" name="category[]' + i + '" placeholder = "category_' + i + '" /&gt;&lt;br/&gt;'; 
            }

            //insert this html code into the div with id catList
            $('#catList').html(textboxes);
        });
    }); 
&lt;/script&gt;


 &lt;form method="post" action=""&gt;
    Number of fields required:      
    &lt;select id="num_cat" name="num_cat"&gt;
        &lt;option value="0"&gt;- SELECT -&lt;/option&gt;
        &lt;option value="1"&gt;1&lt;/option&gt;
        &lt;option value="2"&gt;2&lt;/option&gt;
        &lt;option value="3"&gt;3&lt;/option&gt;
        &lt;option value="4"&gt;4&lt;/option&gt;
        &lt;option value="5"&gt;5&lt;/option&gt;
        &lt;option value="6"&gt;6&lt;/option&gt;
        &lt;option value="7"&gt;7&lt;/option&gt;
        &lt;option value="8"&gt;8&lt;/option&gt;
        &lt;option value="9"&gt;9&lt;/option&gt;
        &lt;option value="10"&gt;10&lt;/option&gt;
    &lt;/select&gt;
    &lt;div id="catList"&gt;&lt;/div&gt;
    &lt;input type="submit" name="submit" value="Submit"/&gt;
&lt;/form&gt;
</code></pre>