duanshaiduhao2471 2016-02-20 13:32
浏览 43
已采纳

使用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; ?>
  • 写回答

2条回答 默认 最新

  • dpzjl68484 2016-02-20 13:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?