PHP遍历表单表并将数据提交到数据库

时间:2018-11-20 21:55:39

标签: php mysql forms loops css-tables

希望有人可以协助解决这个问题,自周末以来一直在关注它。 我有一个表单,其中包含3个字段(名称,电子邮件,季度)和一个包含多个问题的表格(出于说明目的,我将代码缩减为仅2个问题。

我希望将3个字段(名称,电子邮件,数字)写入第1行第1题和第2行第2题的数据库。

问题是我的代码仅将3个字段(名称,电子邮件,数字)写入第1行(问题1),并为3个字段(名称,电子邮件,数字)向第2行写入空白,表字段成功写入。我在做什么错了?

数据库视图

名称电子邮件号码KPICode响应注释 Joe Joe@test.com 555 k21是是测试                         554 k45没有没有测试

HTML代码:

        <div class="item form-group">
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="hfname">Name</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <input type="text" id="name" name="name[]" class="form-control col-md-7 col-xs-12">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email:</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <Select type="email" id="email" name="email[]" class="form-control col-md-7 col-xs-12"></select>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="quarter">Quarter:</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <select id="quarter" name="quarter[]" class="form-control">
                        <option>Choose option</option>
                        <option>Q1</option>
                        <option>Q2</option>
                        <option>Q3</option>
                        <option>Q4</option>
                    </select>
                </div>
            </div>
        </div>
            <table class="table table-bordered">
                <thead class="headings">
                    <tr>
                        <th>Code</th>
                        <th>Category</th>
                        <th>Performance Indicator</th>
                        <th>Response</th>
                        <th>Compliance</th>
                        <th>Notes</th>
                    </tr>
                </thead>

                <tbody>
                    <tr>
                      <td id="env1" name ="code[]" type="text" scope="row">ENV1</td>
                        <td>1.1 KPI</td>
                        <td>KPI Description.</td>
                        <td><select id="response" name="response[]">
                                        <option>Yes</option>
                                        <option>No</option>
                                    </select>
                        </td>
                        <td><select id="Compliance" name="compliance[]">
                                        <option>Compliant</option>
                                        <option>Partially Compliant</option>
                                        <option>Non-Compliant</option>
                                        </select></td>
                        <td><input id="notes" name="notes[]" role="row" Type="text"></td>
                    </tr>                      
                    <tr>

                        <th id="env2" name="code[]" scope="row">ENV2</th>
                        <td>1.2 KPI</td>
                        <td>KPI Description.</td>
                        <td><select id="response" name="response[]">
                                            <option>Yes</option>
                                            <option>No</option>
                                        </select>
                        </td>
                        <td><select id="Compliance" name="compliance[]">
                                            <option>Compliant</option>
                                            <option>Partially Compliant</option>
                                            <option>Non-Compliant</option>
                                            </select></td>
                        <td><input id="notes" name="notes[]" role="row" Type="text"></td>
                    </tr>
                </tbody>
            </table>

我的php代码将信息写入数据库:

foreach($_POST['response'] as $index => $val)
{
$response = $val;
$rfname = $_POST['name'][$index];
$HospitalName = $_POST['email'][$index];
$quarter = $_POST['quarter'][$index];
$kpicode = $_POST['code'][$index];
$compliance = $_POST['compliance'][$index];
$notes = $_POST['notes'][$index];

$sql = "INSERT INTO InfoForm (Name, Email, Quarter, KPICode, Response,  
Compliance, Notes) 
VALUES ('$name', '$email', '$quarter', '$kpicode', '$response',
'$compliance', '$notes')";
$result = mysqli_query($conn, $sql);
}

0 个答案:

没有答案