简体   繁体   English

PHP从输入插入到mysql变量值

[英]Php insert to mysql variable values from inputs

How to add to mysql eg 4 fields and another time eg 120 fields?如何添加到 mysql 例如 4 个字段和另一个时间例如 120 个字段? 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.我的意思是我得到了创建输入的脚本,我想将它添加到 mysql,但在 mysql 中我只能添加与我设置的一样多的值。

There example : id , name , 1 , 2 , 3 but I want that these values ​​are not limited in 1 record.有例子: id , name , 1 , 2 , 3但我希望这些值不限于 1 条记录。

And example : set id , name , 1 , 2 , 3 , ... , 2000 in my script who create inputs.例如:在我创建输入的脚本中设置id , name , 1 , 2 , 3 , ... , 2000

<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.正如@TimMorton 已经评论过您对数据库的看法有点错误。

From my understanding of your question:根据我对你的问题的理解:
You've got something with a name and something that gets generated that belongs to it.你有一些有name东西和一些属于它的东西。
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 ).如您所见,每个生成的对象都有一个fk_mainmain 的外键),它显示了它所属的位置( 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/ http://www.mysqltutorial.org/mysql-foreign-key/

In the end, you can use queries with joins to bring that data together.最后,您可以使用带有joins查询将这些数据整合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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