php通过表单表循环并将数据提交到数据库

ercv8c1e  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(250)

希望有人能帮上忙,从周末就开始关注了。我有一个表单,其中包含3个字段(姓名、电子邮件、季度)和一个包含多个问题的表(为了便于说明,我将代码减少到只有2个问题)。
我想把3个字段(姓名,电子邮件,号码)写入数据库,问题1在第1行,问题2在第2行。
问题是我的代码只将3个字段(姓名、电子邮件、号码)写入第1行(问题1),并将3个字段(姓名、电子邮件、号码)的空格写入第2行,表字段写入成功。我做错什么了?
数据库视图
姓名电子邮件号码kpicode回复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);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题