我在各自的文本输入中显示SessionName,SessionDate和SessionTime时遇到问题。应该发生的是用户想从下拉菜单中选择一个会话(评估)。现在,当他们提交下拉菜单时,会话的详细信息(SessionName,SessionDate和SessionTime)应显示在其文本输入中。但相反,我收到了未定义的变量错误,如下所示:
注意:未定义的变量:第243行的dbSessionName ...
注意:未定义的变量:dbSessionDate in ... on line 244
注意:未定义的变量:第245行的dbSessionTime ...
如何将SessionName,SessionTime和SessionDate显示在各自的文本输入中?
以下是代码:
$sessionquery = "
SELECT SessionId, SessionName, 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['modules']);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
if($sessionnum ==0){
echo "<p>Sorry, You have No Assessments under this Module</p>";
} else {
$sessionHTML = '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while ( $sessionqrystmt->fetch() ) {
$sessionHTML .= sprintf("<option value='%s'>%s - %s - %s</option>", $dbSessionId, $dbSessionName, $dbSessionDate, $dbSessionTime) . PHP_EOL;
}
$sessionHTML .= '</select>';
$assessmentform = "<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post' onsubmit='return sessionvalidation();'>
<p>Assessments: {$sessionHTML} </p>
<p><input id='sessionSubmit' type='submit' value='Submit Assessment' name='sessionSubmit' /></p>
<div id='sessionAlert'></div>
</form>";
echo $assessmentform;
}
}
if (isset($_POST['sessionSubmit'])) {
$currentsession = "form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post'>
<p>Current Assessment's Date/Start Time:</p>
<p>Assessment: <input type='text' id='currentAssessment' name='Assessmentcurrent' readonly='readonly' value='{$dbSessionName}'/> </p> //Line 243
<p>Date: <input type='text' id='currentDate' name='Datecurrent' readonly='readonly' value='{$dbSessionDate}'/> </p> //Line 244
<p>Start Time: <input type='text' id='currentTime' name='Timecurrent' readonly='readonly' value='{$dbSessionTime}'/> </p> //Line 245
</form>
";
echo $currentsession;
}
更新
下面的代码可以这样做:
if (isset($_POST['sessionSubmit'])) {
$sessionquery = "
SELECT SessionId, SessionName, 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['modules']);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$currentsession = "<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post'>
<p>Current Assessment's Date/Start Time:</p>
<p>Assessment: <input type='text' id='currentAssessment' name='Assessmentcurrent' readonly='readonly' value='{$dbSessionName}'/> </p>
<p>Date: <input type='text' id='currentDate' name='Datecurrent' readonly='readonly' value='{$dbSessionDate}'/> </p>
<p>Start Time: <input type='text' id='currentTime' name='Timecurrent' readonly='readonly' value='{$dbSessionTime}'/> </p>
<input type='hidden' id='hiddenId' name='hiddenAssessment' value='{$dbSessionId}'/>
</form>
";
echo $currentsession;
}
我需要store_result();在这种情况下?
答案 0 :(得分:0)
您实际上并未使用session
的已发布值从数据库中检索所选会话。
您正在显示的sql用于生成select
框并以发布的形式获取所选的session
,您需要在
if (isset($_POST['sessionSubmit'])) {
...
}
部分使用$_POST['session']
获取之前选择的会话,可能类似于:
$sessionquery = "
SELECT SessionId, SessionName, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(SessionId = ?)
";
....
$sessionqrystmt->bind_param("i",$_POST['session']); // assuming the session ID is an integer