简体   繁体   中英

Saving a multidimensional array into a mysql database

I am working on a online time tracking web page. But i am stuck at the part on transferring the data into the database.

 <?php /* This loop will iterate through all days. */ foreach($_POST["startTime"] as $day=>$startTimes){ /* This loop will give start & end times for a particular day, ie $day */ foreach($startTimes as $timeIndex=>$startTime){ $endTime = $_POST["endTime"][$day][$timeIndex]; if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else { $sql = "INSERT INTO timetableschedule (name, day, startTime, endTime) ". "VALUES ('$name', '$day', '$startTime', '![enter image description here][1]$endTime')"; if (!mysqli_query($con, $sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); } } } ?> 

The form looks like this,

在此处输入图片说明

 <table id="dataTable" class="form-control"> <label for="Monday">Monday</label> <input type="button" value="Add Schedule" onClick="addRow('dataTable')" /> <tbody> <tr> <p> <td> <label>Start Time</label> <input type="text" class="form-control" name="startTime[1][]"> </td> <td> <label>End Time</label> <input type="text" class="form-control" name="endTime[1][]"> </td> </tr> </tbody> </table> </div> </div> <div class="form-group"> <div class="col-sm-7"> <table id="dataTable1" class="form-control"> <label for="Monday">Tuesday</label> <input type="button" value="Add Schedule" onClick="addRow('dataTable1')" /> <tbody> <tr> <p> <td> <label>Start Time</label> <input type="text" class="form-control" name="startTime[2][]"> </td> <td> <label>End Time</label> <input type="text" class="form-control" name="endTime[2][]"> </td> </tr> </tbody> </table> </div> </div> <div class="form-group"> <div class="col-sm-7"> <table id="dataTable2" class="form-control"> <label for="Monday">Wednesday</label> <input type="button" value="Add Schedule" onClick="addRow('dataTable2')" /> <tbody> <tr> <p> <td> <label>Start Time</label> <input type="text" class="form-control" name="startTime[3][]"> </td> <td> <label>End Time</label> <input type="text" class="form-control" name="endTime[3][]"> </td> </tr> </tbody> </table> </div> </div> <div class="form-group"> <div class="col-sm-7"> <table id="dataTable3" class="form-control"> <label for="Monday">Thursday</label> <input type="button" value="Add Schedule" onClick="addRow('dataTable3')" /> <tbody> <tr> <p> <td> <label>Start Time</label> <input type="text" class="form-control" name="startTime[4][]"> </td> <td> <label>End Time</label> <input type="text" class="form-control" name="endTime[4][]"> </td> </tr> </tbody> </table> </div> </div> <div class="form-group"> <div class="col-sm-7"> <table id="dataTable4" class="form-control"> <label for="Monday">Friday</label> <input type="button" value="Add Schedule" onClick="addRow('dataTable4')" /> <tbody> <tr> <p> <td> <label>Start Time</label> <input type="text" class="form-control" name="startTime[5][]"> </td> <td> <label>End Time</label> <input type="text" class="form-control" name="endTime[5][]"> </td> </tr> </tbody> </table> 

The database should look like this,

在此处输入图片说明

But the only the first row of data managed to enter the database. I am not sure where when wrong with my php codes.

Change your html to this

                 <td>
                          <label>Start Time</label>
                          <input type="text" class="form-control" name="time[0]['start']">
                      </td>
                      <td>
                          <label>End Time</label>
                          <input type="text" class="form-control" name="time[0]['end']">
                      </td>

Rest indices will be

time[1]['start']
time[1]['end']

and so on

Then your php code will be easier to read

foreach($_POST['time'] as $day => $time) {
      $sql = "INSERT INTO timetableschedule (name, day, startTime, endTime) ". 
                       "VALUES ('$name', '$day', '" . $time['start'] . "', '" . $time['end'] . "')";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM