简体   繁体   中英

Php insert to mysql variable values from inputs

How to add to mysql eg 4 fields and another time eg 120 fields? I mean I got script who create inputs, and I want to add that to mysql, but in mysql I can only add as many values ​​as I set.

There example : id , name , 1 , 2 , 3 but I want that these values ​​are not limited in 1 record.

And example : set id , name , 1 , 2 , 3 , ... , 2000 in my script who create inputs.

<form method="POST" action="api.php">
  <input type="text" name="name[]" class="admininput">
  <input type="text" name="image[]" class="admininput">
  <input type="hidden" name="movie[]" value="0" class="admininput">
  <input type="text" name="seasioncount[]" class="admininput"> 
  <div class="inputs">
    <input type="text" name="sectioncount[]" class="admininput">        
    <button class="add_form_field">Add New Field &nbsp; <span style="font-size:16px; font-weight:bold;">+ </span></button>
  </div>
  <input type="submit" name="submit" class="button" value="submit"/>
</form>
<script>
$(document).ready(function() {
    var max_fields      = 1000;
    var wrapper         = $(".inputs"); 
    var add_button      = $(".add_form_field"); 

    var x = 1; 
    $(add_button).click(function(e){ 
        e.preventDefault();
        if(x < max_fields){ 
            x++; 
            $(wrapper).append('<div><input type="text" name="serial[]" class="admininput""/><a href="#" class="delete">Delete</a></div>'); //add input box
        }
        else
        {
        alert('You Reached the limits')
        }
    });

    $(wrapper).on("click",".delete", function(e){ 
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>

Just as @TimMorton already commented your thinking about the database is a bit wrong.

From my understanding of your question:
You've got something with a name and something that gets generated that belongs to it.
So a good setup of tables would be like:

Main table

+----+------+
| id | name |
+----+------+
|  1 | foo  |
|  2 | bar  |
|  3 | baz  |
+----+------+

Generated objects

+----+--------------+---------+
| id | random_field | fk_main |
+----+--------------+---------+
|  1 |        12345 |       1 |
|  2 |          123 |       1 |
|  3 |          455 |       2 |
|  4 |         7677 |       2 |
|  5 |          952 |       3 |
+----+--------------+---------+

As you can see, every generated object has a fk_main ( foreign-key to main ) which shows where it belongs to ( generated.fk_main == main.id ).

This way you can save as much relational data as you want.

Maybe have a look into this:
http://www.mysqltutorial.org/mysql-foreign-key/

In the end, you can use queries with joins to bring that data together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM