提问者:小点点

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


我正在尝试将数据从一个html表单保存到MySQL数据库。我对web dev和JavaScript还是个新手。我有一个表单,用户可以在其中添加行字段,这取决于他们需要多少行。表单可以成功地添加和删除行,但问题是检索所有行字段。当我在form_process php文件中使用$_post时,我只能访问最后一行的详细信息。如何检索用户将要创建的所有行字段,以便将它们保存到数据库?

我试过这些帖子,但它们并不涉及动态表单字段:规范:如何将HTML表单数据保存到MySQL数据库和动态表单字段创建和保存到数据库php MySQL

下图显示了表单中有3行字段。如何检索所有行值,以便将它们保存到MySQL数据库的表中?非常感谢你提前抽出时间

我的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>

处理表单的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();
    ?>

创建/添加额外行的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.");

}
}

共1个答案

匿名用户

您可以通过在末尾添加[]来定义要填充为PHP中数组的HTML表单字段。

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

上面的结果是$_post['bus_number']在PHP中是一个数组,您可以从那里处理它。

相关问题