简体   繁体   中英

Having problems with sessions and pages in php

I really don't understand what I am doing here. I have this page profesor.php in which I want to insert some data into the database. After I submit the data from the form I want to be redirected to another page insert.php and display a message. So I have 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>

and 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;
}

My problem is that insert.php doesn't display anything. I really don't understand what I'm doing wrong. Need some help.

your problem is in your form:

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

you send data to insert.php but all the 'magic' with

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

you keep in profesor.php

Just change action="insert.php" to action="profesor.php" and it should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM