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; ?>