2015-07-04 118 views
1

我正在开发一个在线时间跟踪网页。但我坚持将数据传输到数据库的部分。将多维数组保存到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 ?>

的形式是这样的,

enter image description here

<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>

数据库应该是这样的,

enter image description here

但唯一的数据的第一行设法进入数据库。我不确定当我的php代码出错的时候。

+0

对不起但是“但是唯一的第一行数据管理d进入数据库。“不清楚。解释更多。当我检查你的表单和保存到数据库的数据时,我不明白什么是错的(ino数据库应该是什么数据)。例如, – zeflex

+0

例如,星期一1230小时 - 1400小时是一行数据,如果还有另一个星期二1300小时--1500小时,它不会出现在数据库中。 – Raymond

+0

对我来说还不是很抱歉......我让其他人试图帮助你! – zeflex

回答

0

更改您的HTML这个

    <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> 

休息指数将

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

那么你的PHP代码会更容易阅读

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

未定义索引:开始,未定义索引:结束 – Raymond

+0

在HTML中,将'['start']'改为'[start]'。 – Hkan

+0

@Hkan只有最后一个数组集才能进入数据库 – Raymond