lundi 27 juillet 2015

Posting dynamic inputs and inserting into MySQL database

This is my schedule.php page, an example of the user selecting the specific day for the time.

enter image description here

Once the user clicks on to add, the confirmation page will show up. But unfortunately i got these errors.

enter image description here

In mySQL database, the values that suppose to belong to "1700 - 1800" appeared on the "1400 - 1600" row.

enter image description here

This is my code for the schedule.php page,

            <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[]"></td>
                                    <td><input type="text" class="form-control" name="endTime[]"></td>
                                    <td><input type="checkbox" name="Monday[]" value="1"></td>
                                    <td><input type="checkbox" name="Tuesday[]" value="1"></td>
                                    <td><input type="checkbox" name="Wednesday[]" value="1"></td>
                                    <td><input type="checkbox" name="Thursday[]" value="1"></td>
                                    <td><input type="checkbox" name="Friday[]" 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> 

My codes for 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);
                }
                ?>

I am suspecting that the unchecked checkbox are causing the problem. Do anyone have any idea to solve it?




Aucun commentaire:

Enregistrer un commentaire