我正在開發一個在線時間跟蹤網頁。但我堅持將數據傳輸到數據庫的部分。將多維數組保存到mysql數據庫中
<?php
\t /* This loop will iterate through all days. */
\t foreach($_POST["startTime"] as $day=>$startTimes){
\t /* This loop will give start & end times for a particular day, i.e. $day */
\t \t \t \t \t
\t foreach($startTimes as $timeIndex=>$startTime){
\t \t $endTime = $_POST["endTime"][$day][$timeIndex];
\t \t if (mysqli_connect_errno($con)) {
\t \t \t \t echo "Failed to connect to MySQL: " . mysqli_connect_error();
\t \t \t } else {
\t \t \t \t $sql = "INSERT INTO timetableschedule (name, day, startTime, endTime) ".
\t \t \t \t \t \t "VALUES ('$name', '$day', '$startTime', '![enter image description here][1]$endTime')";
\t \t \t \t
\t \t \t \t \t \t if (!mysqli_query($con, $sql)) {
\t \t \t \t \t \t \t die('Error: ' . mysqli_error($con));
\t \t \t \t \t \t }
\t \t \t \t \t \t echo "1 record added";
\t \t \t \t \t \t mysqli_close($con);
\t \t }
\t }
} \t \t \t \t \t
\t \t \t \t ?>
的形式是這樣的,
<table id="dataTable" class="form-control">
\t <label for="Monday">Monday</label>
\t \t \t \t \t \t <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">
\t <label for="Monday">Tuesday</label>
\t \t \t \t \t \t <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">
\t <label for="Monday">Wednesday</label>
\t \t \t \t \t \t <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">
\t <label for="Monday">Thursday</label>
\t \t \t \t \t \t <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">
\t <label for="Monday">Friday</label>
\t \t \t \t \t \t <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>
\t \t \t \t \t \t </table>
數據庫應該是這樣的,
但唯一的數據的第一行設法進入數據庫。我不確定當我的php代碼出錯的時候。
對不起但是「但是唯一的第一行數據管理d進入數據庫。「不清楚。解釋更多。當我檢查你的表單和保存到數據庫的數據時,我不明白什麼是錯的(ino數據庫應該是什麼數據)。例如, – zeflex
例如,星期一1230小時 - 1400小時是一行數據,如果還有另一個星期二1300小時--1500小時,它不會出現在數據庫中。 – Raymond
對我來說還不是很抱歉......我讓其他人試圖幫助你! – zeflex