【问题标题】:Saving a multidimensional array into a mysql database将多维数组保存到mysql数据库中
【发布时间】:2015-09-22 08:08:12
【问题描述】:

我正在开发一个在线时间跟踪网页。但我被困在将数据传输到数据库的部分。

<?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);
		    }
	    }        
    }					  

				  ?>

表格是这样的,

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

数据库应该是这样的,

但只有第一行数据设法进入数据库。我不确定我的 php 代码哪里出错了。

【问题讨论】:

  • 抱歉,“但是只有第一行数据成功进入数据库。”不清楚。解释更多。当我检查您的表单和保存到 db 中的数据时,我不明白出了什么问题(ino db 应该是什么数据)。
  • 例如,星期一 1230hrs - 1400hrs 是一行数据,如果有另一个星期二 1300hrs - 1500hrs 则不会出现在数据库中。
  • 我还不清楚对不起...我让其他人试图帮助你!
  • 我想我缺少一些循环编码

标签: php mysql arrays database multidimensional-array


【解决方案1】:

把你的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'] . "')";
}

【讨论】:

  • 未定义索引:开始,未定义索引:结束
  • 在 HTML 中,将 ['start'] 更改为 [start]
  • @Hkan 只有最后一个数组集才能进入数据库
  • 确保您的输入不是全部time[0][start],并且数字是递增的。比如,第一个开始时间输入是time[0][start],第二个开始时间输入是time[1][start],第三个是time[2][start]等等。
猜你喜欢
  • 2016-06-14
  • 1970-01-01
  • 2012-09-29
  • 2015-11-24
  • 1970-01-01
  • 2014-01-12
  • 2017-11-12
  • 1970-01-01
  • 2015-04-21
相关资源
最近更新 更多