我正在尝试检索考试(会话),具体取决于教师从上一个下拉菜单中选择的模块。
例如:
从下拉列表中选择的模块: CHI2332(ModuleId)高级数据库系统(ModuleName)
考试(会话)显示在下拉列表中:
AAA(SessionId)24-04-2012(SessionDate)11:00:00(会话时间)
ARF(SessionId)13-06-2012(SessionDate)14:00:00(会话时间)
EFT(SessionId)10-09-2012(SessionDate)09:00:00(会议时间)
它应该在下拉菜单中显示上面的考试,因为这些考试与从模块下拉菜单中选择的moduleId相匹配,而这些考试的TeacherId属于登录到此页面的教师用户名。
问题是,在考试下拉菜单中,即使模块对于这些考试是合法的,也似乎无法找到任何考试,因此它应该在下拉菜单中显示这些考试。
它只是输出以下错误:
Sessions: Notice: Undefined variable: sessionHTML in /.../ on line 178
如何让考试出现在下拉菜单中?
以下是数据库表:
会话表:
SessionId SessionDate SessionTime ModuleId
AAA 24-04-2012 11:00:00 CHI2332
ARF 13-06-2012 14:00:00 CHI2332
EFT 10-09-2012 09:00:00 CHI2332
模块表:
ModuleId ModuleName
CHI2332 Advanced Database Systems
下面是php / mysqli代码:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$courseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : '';
?>
<h1>DELETING AN ASSESSMENT</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Course ID: <input type="text" name="courseid" /><input id="courseSubmit" type="submit" value="Submit" name="submit" /></p>
</form>
<?php
if (isset($_POST['submit'])) {
$query = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = ?)
ORDER BY c.CourseName, m.ModuleId
";
$qrystmt=$mysqli->prepare($query);
// You only need to call bind_param once
$qrystmt->bind_param("s",$courseid);
// get result and assign variables (prefix with db)
$qrystmt->execute();
$qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);
$qrystmt->store_result();
$num = $qrystmt->num_rows();
if($num ==0){
echo "<p>Sorry, No Course was found with this Course ID '$courseid'</p>";
} else {
$dataArray = array();
while ( $qrystmt->fetch() ) {
// data array
$dataArray[$dbCourseId]['CourseName'] = $dbCourseName;
$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName;
// session data
$_SESSION['idcourse'] = $dbCourseId;
$_SESSION['namecourse'] = $dbCourseName;
}
foreach ($dataArray as $courseId => $courseData) {
$output = "";
$output .= "<p><strong>Course:</strong> " . $courseId . " - " . $courseData['CourseName'] . "</p>";
$moduleHTML = "";
$moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($courseData['Modules'] as $moduleId => $moduleData) {
$moduleHTML .= "<option value='$moduleId'>" . $moduleId . " - " . $moduleData['ModuleName'] ."</option>".PHP_EOL;
}
}
$moduleHTML .= '</select>';
echo $output;
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Module: <?php echo $moduleHTML; ?><input id="moduleSubmit" type="submit" value="Submit" name="modulesubmit" /></p>
</form>
<?php
}
if (isset($_POST['modulesubmit'])) {
var_dump($_POST['modulesDrop']);
$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(ModuleId = ?)
ORDER BY SessionDate, SessionTime
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("s",$_POST['modulesDrop']);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
$dataArraySession = array();
while ( $sessionqrystmt->fetch() ) {
$dataArraySession[$dbModuleId]['Sessions'][$dbSessionId]['SessionDate'] = $dbSesisonDate['SessionTime'] = $dbSessionTime;
}
foreach ($dataArraySession as $sessionId => $sessionData) {
$sessionHTML = "";
$sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$sessionHTML .= "<option value='$sessionId'>" . $sessionId . " - " . $sessionData['SessionDate']. " - " . $sessionData['SessionTime'] ."</option>".PHP_EOL;
$sessionHTML .= '</select>';
}
if ($sessionnum > 0) {
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Sessions: <?php echo $sessionHTML; ?><input id="sessionSubmit" type="submit" value="Submit" name="sesionsubmit" /></p>
</form>
<?php
}
else {
echo "<p>Sorry, You have No Sessions under this Module</p>";
}
}
?>
应用程序链接位于:Application
答案 0 :(得分:1)
我在这里看到一个拼写错误(第111行):
$dataArraySession[$dbSessionId]['Sessions']['SessionDate'] = $dbSesisonDate['SessionTime'] = $dbSesisonTime;
您绑定的参数名为$dbSessionTime
,您正在为$dbSesisonTime
数组分配$dataArraySession
。这将导致$dataArraySession
不被第109行的while循环中的任何值填充。随后,第115行中的foreach
循环将永远不会被执行,因为$sessionHTML
在此循环内定义,在第131行
PS:您提供的应用程序链接需要进行身份验证才能继续。
编辑,以便在特定courseId
没有数据的情况下提供代码示例:
在130-132
语句中包装行if
,以确保只有在找到某些结果时才会显示下拉列表
if ($sessionnum > 0) {
// display your form
}
else {
echo "No results found!";
}
答案 1 :(得分:0)
更改$sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
至$sessionHTML .= '<select name="sessionsDrop" id="sessionsDrop">'.PHP_EOL;
在POST中传递的名称不是id
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$courseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : '';
?>
<h1>DELETING AN ASSESSMENT</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Course ID: <input type="text" name="courseid" /><input id="courseSubmit" type="submit" value="Submit" name="submit" /></p>
</form>
<?php
if (isset($_POST['submit'])) {
$query = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = ?)
ORDER BY c.CourseName, m.ModuleId
";
$qrystmt=$mysqli->prepare($query);
// You only need to call bind_param once
$qrystmt->bind_param("s",$courseid);
// get result and assign variables (prefix with db)
$qrystmt->execute();
$qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);
$qrystmt->store_result();
$num = $qrystmt->num_rows();
if($num ==0){
echo "<p>Sorry, No Course was found with this Course ID '$courseid'</p>";
} else {
$dataArray = array();
while ( $qrystmt->fetch() ) {
// data array
$dataArray[$dbCourseId]['CourseName'] = $dbCourseName;
$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName;
// session data
$_SESSION['idcourse'] = $dbCourseId;
$_SESSION['namecourse'] = $dbCourseName;
}
foreach ($dataArray as $courseId => $courseData) {
$output = "";
$output .= "<p><strong>Course:</strong> " . $courseId . " - " . $courseData['CourseName'] . "</p>";
$moduleHTML = "";
$moduleHTML .= '<select name="modulesDrop" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($courseData['Modules'] as $moduleId => $moduleData) {
$moduleHTML .= "<option value='$moduleId'>" . $moduleId . " - " .
$moduleData['ModuleName'] ."</option>".PHP_EOL;
}
}
$moduleHTML .= '</select>';
echo $output;
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Module: <?php echo $moduleHTML; ?><input id="moduleSubmit" type="submit" value="Submit" name="modulesubmit" /></p>
</form>
<?php
}
if (isset($_POST['modulesubmit'])) {
var_dump($_POST['modulesDrop']);
$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(ModuleId = ?)
ORDER BY SessionDate, SessionTime
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("s",$_POST['modulesDrop']);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
$dataArraySession = array();
while ( $sessionqrystmt->fetch() ) {
$dataArraySession[$dbSessionId]['SessionDate'] = $dbSessionDate;
$dataArraySession[$dbSessionId]['SessionTime'] = $dbSessionTime;
}
foreach ($dataArraySession as $sessionId => $sessionData) {
$sessionHTML = "";
$sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$sessionHTML .= "<option value='$sessionId'>" . $sessionId . " - " . $sessionData['SessionDate']. " - " . $sessionData['SessionTime'] ."</option>".PHP_EOL;
$sessionHTML .= '</select>';
}
if ($sessionnum > 0) {
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Sessions: <?php echo $sessionHTML; ?><input id="sessionSubmit" type="submit" value="Submit" name="sesionsubmit" /></p>
</form>
<?php
}
else {
echo "<p>Sorry, You have No Sessions under this Module</p>";
}
}
?>