【发布时间】:2016-10-03 20:18:08
【问题描述】:
当我重新加载/刷新表单时数据消失。
我的数据查询..
public function view_detail()
{
$qry=$this->conn->prepare("SELECT * FROM student_detail WHERE Student_Cnic=:search OR Student_Name=:search");
$qry->bindParam(':search',$this->stsearch);
$qry->execute();
return $qry;
}
我发送数据的表单
<form action="view.php" method="post" id="view_form">
<br>
<label for="">View By Name or CNIC</label>
<input type="text" class="form-control" name="stu-view" id="stu-view" placeholder="Student Name or CNIC"><br>
<input type="submit" name="view-detail" id="view-detail" class="btn btn-success" value="Enroll"><br>
</form>
我填充数据的表单
<?php
include 'header.php';
include 'config.php';
include 'classes.php';
$database=new Database();
$db=$database->getConnection();
$gtstu=new stu_sys($db);
if(isset($_POST['stu-view']))
{
$_SESSION['stusearch'] = $_POST['stu-view'];
if(isset($_SESSION['stusearch'])){
$gtstu->stsearch = $_SESSION['stusearch'];
}
}
$fth = $gtstu->view_detail();
?>
<div class="row">
<div class="col-lg-12">
<div class="col-lg-2"></div>
<div class="col-lg-8" style="margin-top: 10%;">
<table class="table table-responsive table-bordered">
<th>Image</th>
<th>Name</th>
<th>Cnic</th>
<th>Department</th>
<th>View Detail</th>
<?php while($row = $fth->fetch(PDO::FETCH_OBJ)):?>
<tr>
<td><img src="images/<?php echo $row->Student_Image; ?>" alt=""/></td>
<td><?php echo $row->Student_Name ?></td>
<td><?php echo $row->Student_Cnic ?></td>
<td><?php echo $row->Deprt ?></td>
<td><a href="#">View More Detail</a></td>
</tr>
<?php endwhile;?>
</table>
<a href="dashboard.php" class="btn btn-primary">Back TO Dashboard</a>
</div>
<div class="col-lg-2"></div>
</div>
</div>
<?php
include 'footer.php'
?>
我不知道为什么表格会变空,意思是当我重新加载页面时数据被擦除了..
任何帮助将不胜感激。
【问题讨论】:
-
当您提交表单时,数据会在 php 服务器上提交,而当您刷新页面时,数据会丢失。为此,您应该使用 session。
-
填充数据的表单永远不会工作,原因很简单:您没有在代码顶部包含
session_start();。所以在页面刷新时,会话被销毁。 -
感谢@icecub 我忘了在页面上添加会话。
-
不客气 :) 这种小错误很容易犯,哈哈