将多维数组保存到mysql数据库中

时间:2015-07-04 19:39:04

标签: php mysql arrays database multidimensional-array

我正在开发一个在线时间跟踪网页。但我仍然坚持将数据传输到数据库中。

<?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, i.e. $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);
		    }
	    }        
    }					  

				  ?>

表单如下所示

enter image description here

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

数据库应如下所示

enter image description here

但是唯一能够进入数据库的第一行数据。我不知道我的php代码出错了。

1 个答案:

答案 0 :(得分: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'] . "')";
}