【问题标题】:Data disappear from from in php after page refresh页面刷新后数据从php中消失
【发布时间】: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 我忘了在页面上添加会话。
  • 不客气 :) 这种小错误很容易犯,哈哈

标签: php oop pdo


【解决方案1】:

填充数据的表单永远不会工作,原因很简单:你没有包括 session_start();在代码的顶部。所以在页面刷新时,会话被销毁。

我的错误:我忘记在页面顶部添加session_start()

【讨论】:

    【解决方案2】:
    if(isset($_POST['stu-view']))
    {
       $_SESSION['stusearch'] = $_POST['stu-view'];     
    }
    
    // Move this out
    if(isset($_SESSION['stusearch'])){
       $gtstu->stsearch = $_SESSION['stusearch'];
        }
    

    我假设您没有重新发送表单中的数据。因此,您没有从会话中获取数据,因为您仅在发送数据时才查看会话,

    【讨论】:

      【解决方案3】:

      假设您正在谈论“发送数据”的表单,您应该向文本框(stu-view)添加一个 value 属性并使用提交的搜索值填充它,即添加:

      value="<?php echo $_SESSION['stusearch']; ?>"
      

      【讨论】:

        猜你喜欢
        • 2014-03-27
        • 1970-01-01
        • 2017-01-04
        • 1970-01-01
        • 2016-06-22
        • 2023-03-20
        • 2021-07-03
        • 2017-11-22
        • 1970-01-01
        相关资源
        最近更新 更多