这与我以前的帖子
有关Session not passed and active session is what it gets
现在这是我的问题。一切都很好,但它没有回应我桌上会议的漏洞内容。
这里是查询timekeepingquery.php
<?php
session_start();
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM timekeeping ");
while($row = mysqli_fetch_array($sql))
if(!empty($row)){
$_SESSION['row'] = $row;
header("location:timekeepinglogs.php");
}
?>
现在这里是(timekeepinglogs.php)
中的表格<table width="10%" border="0" cellspacing="1" cellpadding="0" style="width: 897px;">
<tr>
<td>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" class="form-table">
<tr>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Full Name</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock In</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Clock Out</strong></td>
<td align="center" bgcolor="#FFFFFF" class="tdclass"><strong>Date</strong></td>
<!--<?php
//while($row = mysqli_fetch_array($sql))
{
?>-->
<tr>
<td bgcolor="#FFFFFF"><?php echo $row['fullname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualstart']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualend']; ?></td>
<td bgcolor="#FFFFFF"><?php echo date("m-d-Y", strtotime($row['createddate'])); ?></td>
</tr>
<!--<?php
}
?>-->
</table>
</td>
</tr>
</table>
但它只在表格中显示了最后一条记录。我希望获得所有记录。
答案 0 :(得分:2)
您是第一次重定向循环。除此之外,你每次都会覆盖$_SESSION['row']
:
while($row = mysqli_fetch_array($sql))
$_SESSION['rows'][] = $row;
}
header("location:timekeepinglogs.php");
exit;
然后你需要遍历$_SESSION['rows']
数组(假设session_start()
中有timekeepinglogs.php
:
<?php
foreach($_SESSION['rows'] as $row)
{
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $row['fullname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualstart']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['actualend']; ?></td>
<td bgcolor="#FFFFFF"><?php echo date("m-d-Y", strtotime($row['createddate'])); ?></td>
</tr>
<?php
}
?>
但是cmorrissey提出了一个好点...你为什么要这样做?
答案 1 :(得分:1)
$sql = mysqli_query($con,"SELECT * FROM timekeeping ");
while($row = mysqli_fetch_array($sql))
if(!empty($row)){
$_SESSION['row'] = $row;
header("location:timekeepinglogs.php");
}
当您修复缩进时,它非常明显。只要您在会话中放置一行,就会重定向。将重定向移动到循环外部并有条件,并在while循环中添加大括号。
编辑:您还需要在多维数组中保存每一行。