简体   繁体   English

检索 HTML 动态表单字段数据并保存到 MySQL 数据库

[英]Retrieving HTML dynamic form fields data and saving to MySQL database

I am trying to save data from a html form to MySQL database.我正在尝试将 html 表单中的数据保存到 MySQL 数据库中。 I am quite new to web dev and javascript.我对 web dev 和 javascript 很陌生。 I have a form in which the user can add row fields depending on how many rows they need.我有一个表单,用户可以在其中添加行字段,具体取决于他们需要多少行。 The form can successfully add and delete rows but the problem is retrieving all row fields.该表单可以成功添加和删除行,但问题是检索所有行字段。 I can access only the last row details when i use $_POST in my form_process php file.当我在 form_process php 文件中使用 $_POST 时,我只能访问最后一行的详细信息。 How can I retrieve all the row fields that the user will create so that I can save them to the database?如何检索用户将创建的所有行字段,以便将它们保存到数据库中?

I tried these posts but they are not dealing with dynamic form fields: Canonical: How to save HTML form data into MySQL database and Dynamic form field creation and saving in database php mysql I tried these posts but they are not dealing with dynamic form fields: Canonical: How to save HTML form data into MySQL database and Dynamic form field creation and saving in database php mysql

The image below shows the form with 3 rows field out.下图显示了带有 3 行字段的表单。 How can I retrieve all the row values so that I save them to a table on a MySQL database?如何检索所有行值,以便将它们保存到 MySQL 数据库上的表中? Thank you very much for your time in advance非常感谢您提前抽出时间

带有条目的动态表单域

My HTML form code is below:我的 HTML 表单代码如下:

        <fieldset class="row2">
        <legend>Bus Data</legend>
        <p> 
            <input type="button" value="Add Bus" onClick="addRow('busDataTable')" /> 
            <input type="button" value="Remove Bus" onClick="deleteRow('busDataTable')"  /> 
            <p>(Remove action applies only to a column with a marked check box.)</p>
        <p>

        <table id="busDataTable" class="form" border="1">
            <tbody>
                <tr>
                    <p>
                        <td><input type="checkbox"  name="chk[]" checked="checked" /></td>
                        <td>
                            <label>Bus #</label>
                            <input type="text" required="required" name="Bus_Number">
                        </td>
                        <td>
                            <label for="Load_kW">Load kW</label>
                            <input type="text" required="required"   name="Load_kW">
                        </td>
                        <td>
                            <label for="Load_kVAR">Load kVAR</label>
                            <input type="text" required="required"   name="Load_kVAR">
                        </td>
                        <td>
                            <label for="Voltage_kV">Voltage kV</label>
                            <input type="text" required="required"   name="Voltage_kV">               
                        </td>
                    </p>
                </tr>
            </tbody>
        </table>

My PHP code that is suppose to process the form: (Here is where the main problem is, I can retrieve for a single row but how do I retrieve for all rows?我的 PHP 代码应该处理表单:(这里是主要问题,我可以检索单行但如何检索所有行?

        <?php
    // put your code here
    // Connecting to mySQL database
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "Radial Distribution System";

    // Create connection to database and get power system data
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // for showing the exact error on the html page
    $conn = new mysqli($servername, $username, $password, $dbname);        


    $_busNumber = array();
    $_busNumber []= $_POST['Bus_Number'];
    print_r($_busNumber);



    $conn->close();
    ?>

The javascript code that creates/adds the extra rows:创建/添加额外行的 javascript 代码:

function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 66){                          // limit the user from creating fields more than your limits
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    for(var i=0; i<colCount; i++) {
        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;
    }
}else{
     alert("Maximum nodes for this platform is 66.");

}
}

You can define the HTML form fields to be populated as arrays in PHP by adding [] to the end.您可以通过在末尾添加[]来定义 HTML 表单字段以填充为 PHP 中的 arrays。

<input type="text" required="required" name="Bus_Number[]">

Above would result in $_POST['Bus_Number'] to be an array in PHP and you can process it from there.以上将导致$_POST['Bus_Number']成为 PHP 中的数组,您可以从那里处理它。

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

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