处理process_student.php页面后,标题位置无效

时间:2017-05-03 06:40:21

标签: php forms

我在addstudent.php页面中的代码

<?php include 'lib/header.php'; ?>

<div class="container mt-2 ">
    <div class="row">
        <div class="col-lg-12">
            <div class="panel ">
                <dev class="panel-heading main-color-bg bg-faded">
                    <h3 class="panel-title">Add Student <span class="pull-right"><a href="index.php" class="btn btn-primary">Back</a></span></h3>
                </dev>
                <div class="panel-body">
                    <form class="form form-control" action="lib/process_student.php" method="POST">
                        <fieldset class="form-group">
                            <label for="name"><strong>Student Name</strong></label>
                            <input type="text" class="form-control" id="name" name="name" placeholder="Student Name" required="1">
                        </fieldset>
                        <fieldset class="form-group">
                            <label for="email"><strong>Student Email</strong></label>
                            <input type="email" class="form-control" id="email" name="email" placeholder="Student Email" required="1">
                        </fieldset>
                        <fieldset class="form-group">
                            <label for="phone"><strong>Student Phone</strong></label>
                            <input type="text" class="form-control" id="phone" name="phone" placeholder="Student Phone" required="1">
                        </fieldset>
                        <fieldset class="form-group">
                            <input type="hidden" name="action" value="add">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" Value="Add Student">
                        </fieldset>
                    </form>
                </div
            </div>
        </div>
    </div>
</div>

<?php include 'lib/footer.php'; ?>

现在,当它进入process_student.php页面时,它就会卡在那里。我的process_student.php

<?php  
include_once 'Session.php'; 
include 'Database.php'; 
Session::init(); 
$db = new Database; 
$tablename = "tbl_student"; 
if(isset($_REQUEST["action"]) && !empty($_REQUEST["action"])) {     
    if($_REQUEST["action"] == "add") {
        $studentdata = ["name" =$_POST["name"],"email" =$_POST["email"],"phone" =$_POST["phone"]];
        $insertdata = $db->insert($tablename,$studentdata);

        if ($insertdata) {          
            $msg = "<h2 class='alert alert-success'><strong>Success! Data inserted successfuly</strong></h2>"; 
            return $msg;
        } else {
            $msg = "<h2 class='alert alert-danger'><strong>Error! No data inserted</strong></h2>";
            return $msg;
        }

        /* redirecting to the index page */         
        Session::set("msg",$msg);       
        $home_url = "../index.php";         
        ob_start();         
        header("Location:".$home_url);      
        exit();
    }elseif ($_REQUEST["action"] == "edit") {
            $id = $_POST["id"];
            if (!empty($id)){
                $studentdata = ["name" =$_POST["name"],"email" =$_POST["email"],"phone" =$_POST["phone"]];
                $tablename = "tbl_student";
                $condition = ["id" =$id];
                $updateddata = $db->update($tablename,$studentdata,$condition);

                if ($updateddata) {
                    $msg = "<h2 class='alert alert-success'><strong>Success! Data updated successfuly </strong></h2>";
                    return $msg;
                } else {
                    $msg = "<h2 class='alert alert-danger'><strong>Error! No data updated </strong></h2>";
                    return $msg;
                }
                try {
                    Session::set("msg",$msg);

                    header("Location:../index.php");
                } catch (Exception $e) {
                    die("sorry".$e->getMessage());
                }       
            }else {
                # code...
            }
    }
}
?>

2 个答案:

答案 0 :(得分:0)

从此部分代码中删除return语句。因为return语句返回而不执行剩余的代码。

 if ($insertdata) {
     $msg = "<h2 class='alert alert-success'><strong>Success! Data inserted successfuly</strong></h2>";
 } else {
     $msg = "<h2 class='alert alert-danger'><strong>Error! No data inserted</strong></h2>";
 }

会工作!!

答案 1 :(得分:0)

删除返回,因为return会退出当前脚本的执行

添加以下代码在addstudent.php中添加此代码

if(isset($_SESSION['msg'])){
   echo $_SESSION['msg'];
   unset($_SESSION['msg']);
}