0

从数据库中检索数据后,如何获取数据结果并使用 mysqli 将它们回显?我在一个表单中有几个回声,将从数据库中检索数据的变量在这些回声中:

   <?php

$session = isset($_POST['session']) ? $_POST['session'] : '';

$sessionquery = "
SELECT s.SessionId, SessionName, SessionDuration, SessionDate, SessionTime, TotalMarks, SessionWeight, 
PenaltyEnabled, s.ModuleId, ModuleNo, ModuleName, StudentId
FROM Penalty p
INNER JOIN Session s ON p.SessionId = s.SessionId
INNER JOIN Module m ON s.ModuleId = m.ModuleId
LEFT JOIN Student_Session ss ON s.SessionId = ss.SessionId
WHERE
(s.SessionId = ?)
";

$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("i",$session);
// get result and assign variables (prefix with db)

$sessionqrystmt->execute(); 

$sessionqrystmt->bind_result($dbSessionId,$dbSessionName, $dbSessionDuration, $dbSessionDate, $dbSessionTime, $dbTotalMarks, $dbSessionWeight, 
$dbPenaltyEnabled, $dbModuleId, $dbModuleNo, $dbModuleName, $dbStudentId);

$sessionqrystmt->store_result();

?>
<form action='results.php' method='post' id='exam'>

 <?php 
while ($sessionqrystmt->fetch()) {
echo "<p><input type='text' id='studentId' name='studentId' value='$dbStudentId' /></p>";
echo "<p>Module: " . $dbModuleNo . " - " . $dbModuleName . "<input type='text' id='moduleId' name='moduleId' value='$dbModuleId' /></p>";
echo "<p>Assessment: " . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "<input type='text' id='sessionId' name='sessionId' value='$dbSessionId' /></p>";

?>
</form>

更新:

<form action='results.php' method='post' id='exam'>

         <?php 
        while ($sessionqrystmt->fetch()) {
        echo "<p><input type='text' id='studentId' name='studentId' value='$dbStudentId' /></p>";
        echo "<p>Module: " . $dbModuleNo . " - " . $dbModuleName . "<input type='text' id='moduleId' name='moduleId' value='$dbModuleId' /></p>";
        echo "<p>Assessment: " . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "<input type='text' id='sessionId' name='sessionId' value='$dbSessionId' /></p>";
    }

        ?>
        </form>

更新:

在视图源中,它根本不输出表单。

4

1 回答 1

1

在您到达那里的代码之后,您需要使用该fetch()方法进行循环。

例如:

while ($sessionqrystmt->fetch()) {
    // Here you can use the bound_results variables
}
于 2013-02-06T13:55:22.963 回答