在php中遇到会话和页面问题

时间:2015-01-09 14:06:31

标签: php mysql

我真的不明白我在这里做什么。我有这个页面profesor.php,我想在其中插入一些数据到数据库中。从表单提交数据后,我希望将其重定向到另一个页面insert.php并显示一条消息。 所以我有profesor.php

<?php
session_start();

if (isset($_SESSION['id'])) {
    $fullname = $_SESSION['name'];
    echo "<h1> Welcome " . $fullname . "</h1>"; 
} else {
    $result = "You are not logged in yet";
}

if (isset($_POST['studname'])) {
    include_once("dbConnect.php");

    $studname = strip_tags($_POST['studname']);
    $course = strip_tags($_POST['course']);
    $grade = strip_tags($_POST['grade']);

    $getStudidStm = "SELECT userid FROM users WHERE name = '$studname'";
    $getStudidQuery = mysqli_query($dbCon, $getStudidStm);
    $row = mysqli_fetch_row($getStudidQuery);
    $studid = $row[0]; 

    $_SESSION['studid'] = $studid;
    $_SESSION['course'] = $course;
    $_SESSION['grade'] = $grade;
    header("Location: insert.php");
}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $fullname ;?></title>
</head>

<body>
<div id="wrapper">
<h2>Insert new grade</h2>
<form id="insertForm" action="insert.php" method="post" enctype="multipart/form-data">
Student: <input type="text" name="studname" /> <br />
Course : <input type="text" name="course" /> <br />
Grade  : <input type="text" name="grade" /> <br />
<input type="submit" value="Insert" name="Submit" />
</form></div>
</form>
</body>
</html>

insert.php

<?php
session_start();

if (isset($_SESSION['studid'])) {
    include_once("dbConnect.php");

    $studid = $_SESSION['studid'];
    $course = $_SESSION['course'];
    $grade = $_SESSION['grade'];
    echo $studid;
    echo $course;
    echo $grade;
}

我的问题是insert.php没有显示任何内容。我真的不明白自己做错了什么。需要一些帮助。

1 个答案:

答案 0 :(得分:5)

您的问题出在您的表单中:

<form id="insertForm" action="insert.php" [...]

您将数据发送到insert.php,但所有的“魔法”都是如此。与

$_SESSION['studid'] = $studid;
$_SESSION['course'] = $course;
$_SESSION['grade'] = $grade;

你保持profesor.php

只需将action="insert.php"更改为action="profesor.php"即可正常使用。