将HTML表数据添加到MySQL数据库中


Add HTML Table data into MySQL DB

我有一个HTML表和一个允许用户添加多行的按钮。

<table id="component_table">
    <thead>
        <tr>
            <th>Component</th>
            <th>Component Type</th>
            <th>Component Thickness</th>
        </tr>
    </thead>
    <tbody id="component_tb">
        <tr>
            <td><?php echo $roofComponentDropDown ?></td>
            <td><?php echo $roofComponentTypeDropDown ?></td> 
            <td><input id="component_thickness" name="component_thickness" type="text"></td>
        </tr>
    </tbody>
</table>
<input type="button" value="+" id="addRows" />

这些是我为表生成的下拉列表

//Queries here to get data for the drop down lists that make up the roof construction table...
$roofComponentQuery = "SELECT * FROM roof_component";
$roofComponentData = mysqli_query($dbc, $roofComponentQuery);
while ($rcRow = mysqli_fetch_array($roofComponentData)) {
         $roofComponentOptions .="<option value='"".$rcRow['roof_component_id']."'">" . $rcRow['roof_component_name'] . "</option>";
}
$roofComponentDropDown = "<select name='selectedRoofComponent' id='selectedRoofComponent' onChange='getComponentType(this)'>
                            <option selected='selected' disabled='disabled' value=''>Select Component</option>
                        " . $roofComponentOptions . "
                        </select></br>";
$roofComponentTypeDropDown = "<select name='selectedComponentType' id='selectedComponentType'>
                                <option class='toggle_control' selected='selected' disabled='disabled' value=''>Select Type</option>
                            </select></br>";

有没有一种方法可以将每个<tr>添加到一个数组中,然后用foreach循环迭代该数组,并将每个<tr>插入到DB中?

也许是这样的?

$tableRow = array(); 
foreach ($tableRow as $row) {
    //insert query
}

给出以[]结尾的<select>元素名称。

$roofComponentDropDown = "<select name='selectedRoofComponent[]' id='selectedRoofComponent' onChange='getComponentType(this)'>
                            <option selected='selected' disabled='disabled' value=''>Select Component</option>
                        " . $roofComponentOptions . "
                        </select></br>";
$roofComponentTypeDropDown = "<select name='selectedComponentType[]' id='selectedComponentType'>
                                <option class='toggle_control' selected='selected' disabled='disabled' value=''>Select Type</option>
                            </select></br>";

然后$_POST的元素将是可以循环使用的数组。

foreach ($_POST['selectedRoofComponent'] as $index => $component) {
    $componentType = $_POST['selectedComponentType'][$index];
    // INSERT $component and $componentType into database
}

首先确保每个<tr>以及随后新创建的<tr>都有一个唯一的行id。然后使用该行id创建数组索引,将名称分配给与表单一起提交的每个<td>中的元素。

也许像<select name='tableRow[rowId][selectedRoofComponent]' id='selectedRoofComponent' onChange='getComponentType(this)'>这样的东西应该在POST数据中为您提供一个具有适当结构的数组。

在这种情况下,rowId需要是一个变量,该变量由javascript生成,或者在页面加载时由php呈现。因此,在php中,它可能看起来像:

"<select name='tableRow[" . $rowId . "][selectedRoofComponent]' id='selectedRoofComponent' onChange='getComponentType(this)'>"

或者如果javascript

"<select name='tableRow[" + rowId + "][selectedRoofComponent]' id='selectedRoofComponent' onChange='getComponentType(this)'>"

为了给您提供比这更具体的东西,我还需要查看javascript。