Return to Snippet

Revision: 69622
at August 2, 2015 00:52 by raymondeu


Initial Code
<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[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>


//handleschedule.php//
<?php
                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 = isset($_POST["Monday"][$key]) ? 1 : 0;
                            $tuesday = isset($_POST["Tuesday"][$key]) ? 1 : 0;
                            $wednesday = isset($_POST["Wednesday"][$key]) ? 1 : 0;
                            $thursday = isset($_POST["Thursday"][$key]) ? 1 : 0;
                            $friday = isset($_POST["Friday"][$key]) ? 1 : 0;
                           

                            $sql = "INSERT INTO timetableschedule ( startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
                                    "VALUES ('$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
                            mysqli_query($con, $sql);
                    }
                    }

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

Initial URL
http://stackoverflow.com/questions/31761272/save-multiple-row-of-checkbox-value-into-database/31761745?noredirect=1#comment51458029_31761745

Initial Description
I have been trying to solve my problems on stackoverflow, but none seems to help. Hopefully someone here could help me.

Initial Title
Save multiple row of checkbox value into database

Initial Tags
database, php

Initial Language
PHP