Save multiple row of checkbox value into database


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. <tbody id="dataTable">
  2. <tr>
  3. <td>
  4. <input type="text" class="form-control" name="startTime[]">
  5. </td>
  6. <td>
  7. <input type="text" class="form-control" name="endTime[]">
  8. </td>
  9. <td>
  10. <input type="checkbox" name="Monday[0]" value="1">
  11. </td>
  12. <td>
  13. <input type="checkbox" name="Tuesday[0]" value="1">
  14. </td>
  15. <td>
  16. <input type="checkbox" name="Wednesday[0]" value="1">
  17. </td>
  18. <td>
  19. <input type="checkbox" name="Thursday[0]" value="1">
  20. </td>
  21. <td>
  22. <input type="checkbox" name="Friday[0]" value="1">
  23. </td>
  24. <td>
  25. <input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" />
  26. </td>
  27. </tr>
  28. </tbody>
  29.  
  30.  
  31. //handleschedule.php//
  32. <?php
  33. if (mysqli_connect_errno($con)) {
  34. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  35. } else {
  36. if ($_POST['startTime']) {
  37. foreach ($_POST["startTime"] as $key => $value) {
  38.  
  39. $endTime = $_POST["endTime"][$key];
  40. $monday = isset($_POST["Monday"][$key]) ? 1 : 0;
  41. $tuesday = isset($_POST["Tuesday"][$key]) ? 1 : 0;
  42. $wednesday = isset($_POST["Wednesday"][$key]) ? 1 : 0;
  43. $thursday = isset($_POST["Thursday"][$key]) ? 1 : 0;
  44. $friday = isset($_POST["Friday"][$key]) ? 1 : 0;
  45.  
  46.  
  47. $sql = "INSERT INTO timetableschedule ( startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
  48. "VALUES ('$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
  49. mysqli_query($con, $sql);
  50. }
  51. }
  52.  
  53. echo "1 record added";
  54. mysqli_close($con);
  55. }
  56. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.