使用php重定向会话返回未定义的变量并调用成员函数错误

i have the following code which comes from another page on submit with a value, and gets the rest of the values from the database. but im getting 2 errors.

Undefined variable: q on line 23

and

Fatal error: Call to a member function fetch() on a non-object on line 23

what am i doing wrong here?

<?php
if(isset($_POST["submit"])){
    $tmpqid = $_POST["mdqid"];
    require_once 'dbconfig.php';

    try {
        $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * 
                FROM as_questions
                WHERE Qid='$tmpqid'";

        $q = $conn->query($sql);
        $q->setFetchMode(PDO::FETCH_ASSOC);


    } catch (PDOException $pe) {
        die("Could not connect to the database $dbname :" . $pe->getMessage());
    }
}
?>

  <?php while ($r = $q->fetch()): ?>

    <?php 
$tempQid = $r['Qid'];
$tempMclass = $r['M_class'];
$rawMclass = $r['M_class'];
$tempSclass = $r['S_class'];
$rawSclass = $r['S_class'];
$tempQuestion = $r['Question'];
$tempAnswer = $r['Answer'];
$tempDoc = $r['Doctor'];
$rawDoc = $r['Doctor'];
$tempTime = $r['Time'];
$tempAtime = $r['A_time'];
?>
        <?php
        session_start();

        $_SESSION['qid'] = $tempQid;
        $_SESSION['mclass'] = $tempMclass;
        $_SESSION['rmclass'] = $rawMclass;
        $_SESSION['sclass'] = $tempSclass;
        $_SESSION['rsclass'] = $tempSclass;
        $_SESSION['question'] = $tempQuestion;
        $_SESSION['answer'] = $tempAnswer;
        $_SESSION['doc'] = $tempDoc;
        $_SESSION['time'] = $tempTime;
        $_SESSION['atime'] = $tempAtimeime;

        header("Location: http://localhost/pro/multipledelete.php");
        exit();
?>
        <?php endwhile; ?>

You need to change your code like below:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
session_start(); // must be first line
if(isset($_POST["submit"])){
    $tmpqid = $_POST["mdqid"];
    require_once 'dbconfig.php';
    try {
        $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM as_questions WHERE Qid= $tmpqid";
        $q = $conn->query($sql);
        $q->setFetchMode(PDO::FETCH_ASSOC);
        // enter your while code in try block
        while ($r = $q->fetch()){
            $tempQid = $r['Qid'];
            $tempMclass = $r['M_class'];
            $rawMclass = $r['M_class'];
            $tempSclass = $r['S_class'];
            $rawSclass = $r['S_class'];
            $tempQuestion = $r['Question'];
            $tempAnswer = $r['Answer'];
            $tempDoc = $r['Doctor'];
            $rawDoc = $r['Doctor'];
            $tempTime = $r['Time'];
            $tempAtime = $r['A_time'];

            $_SESSION['qid'] = $tempQid;
            $_SESSION['mclass'] = $tempMclass;
            $_SESSION['rmclass'] = $rawMclass;
            $_SESSION['sclass'] = $tempSclass;
            $_SESSION['rsclass'] = $tempSclass;
            $_SESSION['question'] = $tempQuestion;
            $_SESSION['answer'] = $tempAnswer;
            $_SESSION['doc'] = $tempDoc;
            $_SESSION['time'] = $tempTime;
            $_SESSION['atime'] = $tempAtimeime;

            header("Location: http://localhost/pro/multipledelete.php");
            exit();
        }
    } catch (PDOException $pe) {
        die("Could not connect to the database $dbname :" . $pe->getMessage());
    }
}

?>

Note:- file extension must be .php and i removed extra <?php and ?> from your code. Not necessary.

Did you try using a foreach instead of a while? Something like this:

<?php 

    $sql = "SELECT * FROM as_questions WHERE Qid='$tmpqid'";

    foreach ($conn->query($sql) as $r) {
        $tempQid = $r['Qid'];
        $tempMclass = $r['M_class'];
        $rawMclass = $r['M_class'];
        $tempSclass = $r['S_class'];
        $rawSclass = $r['S_class'];
        $tempQuestion = $r['Question'];
        $tempAnswer = $r['Answer'];
        $tempDoc = $r['Doctor'];
        $rawDoc = $r['Doctor'];
        $tempTime = $r['Time'];
        $tempAtime = $r['A_time'];
    }

?>