简体   繁体   English

PHP 表单插入带有动态输入的数据数组

[英]PHP Form Insert Array of Data with Dynamic Inputs

Using PHP I'm building a web form that will allow people to submit details of network devices registered to them, these details are saved in a mysql database.使用 PHP 我正在构建一个 Web 表单,允许人们提交注册给他们的网络设备的详细信息,这些详细信息保存在 mysql 数据库中。 I want to allow users to submit multiple devices on the same form.我想允许用户在同一个表单上提交多个设备。

The details I need to collect are;我需要收集的详细信息是;

  • make制作
  • model模型
  • mac address MAC地址

A user may need to register 20 identical devices, ie the same make and model, the only field that will change is mac address.一个用户可能需要注册 20 个相同的设备,即相同的品牌和型号,唯一会改变的字段是 mac 地址。 Rather than ask users to submit 20 separate forms I have a button 'Add Similar Device', using JQuery this creates a new mac address input (array type).我没有要求用户提交 20 个单独的表单,我有一个按钮“添加类似设备”,使用 JQuery 创建一个新的mac 地址输入(数组类型)。

All is working as expected but I'm not sure if i'm doing it 'the right way', so I was hoping for some advice - my code is below;一切都按预期工作,但我不确定我是否以“正确的方式”做这件事,所以我希望得到一些建议 - 我的代码如下;

HTML HTML

<form id="myForm" method="POST" action="">
    <input id="make" name="make" type="text">
    <input id="model" name="model" type="text">
    <input id="mac[]" name="mac[]" type="text">
    <!-- example of two dynamically added mac inputs
    <input id="mac[]" name="mac[]" type="text">
    <input id="mac[]" name="mac[]" type="text">
    -->
    <input id="submit" name="submit" type="submit">
</form>
<button class="add-similar" id="add-similar" type="button"></button>

JQuery查询

<script> 
$(document).ready(function() {
    var maxField = 10; 
    var addButton = $('.add-similar'); 
    var html = '<input id="mac[]" name="mac[]" type="text">';
    var x = 1;
    $(addButton).on('click', function(e) {
        if (x < maxField) {
            x++;
            $(wrapper).append(html); 
        }
    });
}); 
</script>

PHP PHP

if(isset($_REQUEST["submit"])){
    $make = $_POST['make'];
    $model = $_POST['model'];
    $mac = $_POST['mac'];
    for ($i = 0; $i < count($mac); $i++) {
        // insert data into db
    }
}

For every additional mac address input dynamically added, I need to create a new, unique record in the db.对于动态添加的每个额外的 mac 地址输入,我需要在 db 中创建一个新的、唯一的记录。 If two dynamic mac addresses are added, the db table should look like this;如果添加两个动态mac地址,db表应该是这样的;

+----+-------+---------+--------------+
| id | make  | model   | mac          |
+----+-------+---------+--------------+
| 1  | Apple | Macbook | 112233445566 |
+----+-------+---------+--------------+
| 2  | Apple | Macbook | 998877665544 |
+----+-------+---------+--------------+
| 3  | Apple | Macbook | 887766225533 |
+----+-------+---------+--------------+

Am I approaching this the right way?我是否以正确的方式接近这个?

Regarding Bulk Insertion: You need to build the sql statement like INSERT INTO table(make, model, mac) VALUES("Apple", "Macbook", 123456),("Apple", "Macbook", 245678),("Apple", "Macbook", 345678);关于批量插入:您需要构建像INSERT INTO table(make, model, mac) VALUES("Apple", "Macbook", 123456),("Apple", "Macbook", 245678),("Apple", "Macbook", 345678); You can get the idea to build the statement from the below code block.您可以从下面的代码块中获得构建语句的想法。 NOTE: Also make sure to sanitize the $make, $model, $mac variables Might help注意:还要确保清理 $make、$model、$mac 变量可能会有所帮助

$make = $_POST['make'];
$model = $_POST['model'];
$mac = $_POST['mac'];


$length = count($mac);
$values = [];
for ($i = 0; $i < $length; $i++) {
    $values[] = '("' . $make . '","' . $model . '",' . $mac[$i] .')';
}

$query = "INSERT INTO table(make, model, mac) VALUES". implode(",", $values);

I have a solution for you.我有一个解决方案给你。 Please use Ajax and FormData.请使用 Ajax 和 FormData。

First, change type submit to button and put id to this button.首先,将 type submit 更改为按钮并将 id 放入此按钮。

Second第二

$(id).on('click', function() {
  var fd = new FromData();

  var count_mac = $('input[name="mac[]"]').length;
  fd.append('count_mac', count_mac);

  $('input[name="mac[]"]').each(function(index){
     var data = $(this).val();

     //Pass to PHP file with $_POST['mac0'],$_POST['mac1'],...
     fd.append('mac'+ index, data);
  });

  //Ajax code here
  jQuery.ajax({
     url: 'example_file.php',
     type: 'post',
     data: fd,
     contentType: false,
     processData: false,
     success: function( results ) {
       location.reload();
     }
  });
});

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

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