发布动态输入并插入MySQL数据库

时间:2015-07-28 00:46:15

标签: php mysql database checkbox multidimensional-array

这是我的schedule.php页面,用户选择当时特定日期的示例。

enter image description here

用户点击添加后,系统会显示确认页面。但不幸的是我遇到了这些错误。

enter image description here

在mySQL数据库中,假设属于“1700 - 1800”的值出现在“1400 - 1600”行中。

enter image description here

这是我的schedule.php页面的代码,

            <form name="Create New schedule"  class="form-horizontal" method="post" action="handleSchedule.php">
                <div class="form-group">
                    <div class="col-sm-4">
                        <label for="AcadInst">Academic Institution</label>

                        <select class="form-control" id="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
                            <option>Institution Name</option>
                            <option>Singapore Polytechnic (SP)</option>
                            <option>Ngee Ann Polytechnic (NP)</option>
                            <option>Temasek Polytechnic (TP)</option>
                            <option>Republic Polytechnic (RP)</option>
                            <option>Nanyang Polytechnic (NYP)</option>
                            <option>Others(Please specify)</option> 
                        </select>
                    </div>
                    <div class="col-sm-4">
                        <label>Level of Teaching</label>
                        <input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
                        <datalist id="LvTeaching">
                            <option value="Undergraduate Teaching">
                            <option value="Postgraduate Teaching">
                            <option value="Continuing Education">
                            <option value="Others (Please specify)"> 
                        </datalist>
                    </div>
                    <div class="col-sm-4">
                        <label>Type of Teaching</label>
                        <input list="TyTeaching" type="text" class="form-control" placeholder="Mode of Teaching" name="typeofteaching">
                        <datalist id="TyTeaching">
                            <option value="Clinical Teaching">
                            <option value="Academic Teaching">
                            <option value="Talk">
                            <option value="Others (Please specify)"> 
                        </datalist>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-4">
                        <label for="startdate">Start Date</label>
                        <input type="date"  class="form-control" placeholder="Select Date" name="startdate">
                    </div>
                    <div class="col-sm-4">
                        <label for="enddate">End Date</label>
                        <input type="date"  class="form-control" placeholder="Select Date" name="enddate" id="noEndDate">
                        <input onclick="document.getElementById('noEndDate').disabled = true;" type="checkbox" name="type">No end date
                    </div>
                </div>

                <div class="form-group">
                    <div class="panel-body table-responsive">
                        <table class="table table-hover table-bordered">
                            <thead>
                                <tr>
                                    <th>Start Time</th>
                                    <th>End Time</th>
                                    <th>Mon</th>
                                    <th>Tue</th>
                                    <th>Wed</th>
                                    <th>Thu</th>
                                    <th>Fri</th>
                                    <th><input type="button" class="btn btn-warning" value="Add Timeslot" onClick="addRow('dataTable')" /></th>
                                </tr>
                            </thead>
                            <tbody id="dataTable">
                                <tr>
                                    <td><input type="text" class="form-control" name="startTime[0]"></td>
                                    <td><input type="text" class="form-control" name="endTime[0]"></td>
                                    <td><input type="checkbox" name="Monday[0]" value="1"></td>
                                    <td><input type="checkbox" name="Tuesday[0]" value="1"></td>
                                    <td><input type="checkbox" name="Wednesday[0]" value="1"></td>
                                    <td><input type="checkbox" name="Thursday[0]" value="1"></td>
                                    <td><input type="checkbox" name="Friday[0]" value="1"></td>
                                    <td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
                                </tr>
                            </tbody>
                        </table>
                        <br/>

                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-4">
                        <input type="submit"  value="Add" class="btn btn-success">
                    </div>
                </div>
            </form> 

我的handleSchedule.php代码,

<?php

    $con = getDbConnect();

    $academicInstitution = $_POST['academicInstitution'];
    $levelofteaching = $_POST['levelofteaching'];
    $typeofteaching = $_POST['typeofteaching'];
    $startdate = $_POST['startdate'];
    $enddate = $_POST['enddate'];

                if (mysqli_connect_errno($con)) {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                } else {
                    if ($_POST['startTime']) {
                    foreach ($_POST["startTime"] as $key => $value) {

                            $endTime = $_POST["endTime"][$key];
                            $monday = $_POST["Monday"][$key];
                            $tuesday = $_POST["Tuesday"][$key];
                            $wednesday = $_POST["Wednesday"][$key];
                            $thursday = $_POST["Thursday"][$key];
                            $friday = $_POST["Friday"][$key];
                           

                            $sql = "INSERT INTO timetableschedule (academicInstitution, lvteaching, tyteaching, startdate, endate, startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
                                    "VALUES ('$academicInstitution', '$levelofteaching', '$typeofteaching', '$startdate', '$enddate', '$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
                            mysqli_query($con, $sql);
                    }
                    }

                    echo "1 record added";
                    mysqli_close($con);
                }
                ?>

我怀疑未选中的复选框导致了问题。有人有任何想法解决它吗?

1 个答案:

答案 0 :(得分:0)

未选中的复选框无法提交给服务器。所以$_POST['Monday']只是所有复选框的数组。这些参数的索引不会与文本框匹配。

解决方案是在名称中放置显式索引:

    <tr>
        <td><input type="text" class="form-control" name="startTime[0]"></td>
        <td><input type="text" class="form-control" name="endTime[0]"></td>
        <td><input type="checkbox" name="Monday[0]" value="1"></td>
        <td><input type="checkbox" name="Tuesday[0]" value="1"></td>
        <td><input type="checkbox" name="Wednesday[0]" value="1"></td>
        <td><input type="checkbox" name="Thursday[0]" value="1"></td>
        <td><input type="checkbox" name="Friday[0]" value="1"></td>
        <td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
    </tr>

添加新行时,需要增加索引。

在PHP中,您需要检查参数是否已设置。

$monday = isset($_POST["Monday"][$key]) ? 1 : 0;