1

I am trying to retrieve exams(sessions) depending on the module the teacher has chosen from the previous drop down menu.

For Example:

Module selected from drop down: CHI2332 (ModuleId) Advanced Database Systems (ModuleName)

Exam(Session) shown in drop down:

AAA (SessionId) 24-04-2012 (SessionDate) 11:00:00 (Session Time)
ARF (SessionId) 13-06-2012 (SessionDate) 14:00:00 (Session Time)
EFT (SessionId) 10-09-2012 (SessionDate) 09:00:00 (Session Time)

It should display the exams above in the drop down menu as these exams match the moduleId chosen from the module dropdown menu and the TeacherId for these exams belong to the Teacher Username logged into to this page.

The problem is that in the Exam drop down menu, it cannot seem to find any exams even though the modules are legitimate for those exams and hence it should display those exams in the drop down menu.

It just outputs this error below in its place:

Sessions: Notice: Undefined variable: sessionHTML in /.../ on line 178

How can I get the exams to appear in the drop down menu when it should do?

Below are the database tables:

Session Table:

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 

Module Table:

ModuleId  ModuleName
CHI2332   Advanced Database Systems

Below is the php/mysqli code:

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


}


        ?>

Link to application is here: Application

4

2 回答 2

1

我在这里看到一个错字(第 111 行):

$dataArraySession[$dbSessionId]['Sessions']['SessionDate'] = $dbSesisonDate['SessionTime'] = $dbSesisonTime;

您绑定的参数被调用$dbSessionTime,并且您正在分配$dbSesisonTime给您的$dataArraySession数组。这将导致$dataArraySession第 109 行的 while 循环中的任何值都不会被填充。随后foreach,第 115 行中的循环将永远不会被执行,并且由于$sessionHTML在此循环中定义,因此稍后在第 131 行中使用它时将未定义

PS:为您的应用程序提供的链接需要身份验证才能继续。

编辑以提供代码示例,以防特定的数据不存在courseId

130-132在语句中换行if,以确保仅在找到某些结果时才显示下拉菜单

if ($sessionnum > 0) {
   // display your form
}
else {
   echo "No results found!";
}
于 2012-10-16T15:40:07.143 回答
0

Change $sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;

To $sessionHTML .= '<select name="sessionsDrop" id="sessionsDrop">'.PHP_EOL;

It is name not id that is passed in POST

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


}


    ?>
于 2012-10-17T21:03:31.823 回答