1

我正在使用您提到的 if $_REQUEST 语句,但我想做的是,如果满足 if 语句,则显示 $echo,否则显示下面的内容(会话下拉菜单和表单)

问题是,即使它显示一个回显,它也会显示一个未定义的通知,说明 sessionnum 在第 179 行未定义。

我的问题是,对于 else 语句,我应该将右括号放在下面的代码中:

if (isset($_POST['modulesubmit'])) {

     if($_REQUEST['modulesDrop']==''){

         echo "Please Select a Module from the Drop Down Menu Above";

     }
     else{  

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

        }
} //closing bracket for else statment
 if ($sessionnum > 0) { //error line is here

 ?>

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

?>
4

1 回答 1

0

您想要的是声明您计划在 IF 条件中使用的任何变量,这些变量将在页面顶部更改/重置其值。

否则,您将收到未定义的索引警告。您也可以尝试使用 @ 来使警告静音。

$sessionnum = 0;

if (isset($_POST['modulesubmit'])) {

     if($_REQUEST['modulesDrop']==''){

         echo "Please Select a Module from the Drop Down Menu Above";

     }
     else{  

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

        }
} //closing bracket for else statment
 if ($sessionnum > 0) { //error line is here

 ?>

     <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-18T21:21:55.927 回答