2013-01-17 54 views
0

在下面的代碼中,我有一個查詢,它從db中查找所有模塊,然後在下拉菜單中將它們列爲選項。現在假設發生的是用戶從下拉菜單中選擇一個模塊並提交。提交後,它將顯示在用戶選擇的模塊下面。選擇下拉選項後顯示錯誤信息

問題是無論用戶從下拉菜單中選擇了哪個模塊,它總是會聲明用戶選擇了Advanced Web programming模塊。我的問題是爲什麼總是說明這一點,而不是選擇顯示正確的模塊?

下面是查看源代碼:

<form action="" method="post">Module: 
    <select name="module" id="modulesDrop"> 
    <option value="">Please Select</option> 
    <option selected='selected' value='CHI2513_Systems Strategy_1'>CHI2513 - Systems Strategy</option> 
    <option value='CHI2565_Ecommerce, Business and Technology_3'>CHI2565 - Ecommerce, Business and Technology</option> 
    <option value='CHT2220_Interactive Systems_4'>CHT2220 - Interactive Systems</option> 
    <option value='CHT2520_Advanced Web Programming_5'>CHT2520 - Advanced Web Programming</option> 
    </select> 
    <input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" /> 
</form> 
<form action="" method="post"> 
    <p> <strong>Selected Module:</strong>CHT2520 - Advanced Web Programming 
    <input type='text' value='CHI2513_Systems Strategy_1' /> 
    </p> 
    Asessments: <span class='red'>Sorry, You have No Assessments under this Module</span> 

下面是寫代碼:

$moduleactive = 1; 
      $sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo"; 

      $sqlstmt=$mysqli->prepare($sql); 
      $sqlstmt->bind_param("i", $moduleactive); 
      $sqlstmt->execute(); 
      $sqlstmt->bind_result($dbModuleId,$dbModuleNo,$dbModuleName); 
      $sqlstmt->store_result(); 
      $sqlnum = $sqlstmt->num_rows(); 
      ?> 
      Module: 
      <select name="module" id="modulesDrop"> 
       <option value="">Please Select</option> 
       <?php 
       while($sqlstmt->fetch()) { 
        $ov = $dbModuleNo . "_" . $dbModuleName . "_" . $dbModuleId; 
        if($ov == $_POST["module"]) 
         echo "<option selected='selected' value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL; 
        else 
         echo "<option value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL; 
       } 
       ?> 
      </select> 
      <input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" /> 
     </form> 
     <?php 
     if(isset($_POST['moduleSubmit'])) 
{ 


if($_POST["module"] != "") { ?> 
     <form action="" method="post"> 
      <p> 
      <strong>Selected Module:</strong><?php echo $dbModuleNo .' - '. $dbModuleName;?> <input type='text' value='<?php echo $_POST["module"];?>'> 
      </p> 
      <?php } ?> 
+0

就問 - 什麼'WHERE ModuleActive =? '聲明的意思? – Kamo

+1

@Kamo?是一個參數。參數存儲在這裏:'$ sqlstmt-> bind_param(「i」,$ moduleactive); '和'$ moduleactive = 1;'。所以WHERE語句是'WHERE ModuleActive = 1' – user1964964

回答

0

我在我的PHP有點生疏,但我相信這是因爲模塊Advanced Web Programming是列表中的最後一個。每次運行while循環從DB中讀取行時,都會更新變量$dbModuleNo$dbModuleName,因此,當您希望顯示所選模塊時,這些變量將包含最近提取的數據,而不是您所需的所選數據。

嘗試是這樣的(我已經添加了幾行現有的代碼):

<?php 
$selectedText = "None"; 
while($sqlstmt->fetch()) { 
    $ov = $dbModuleNo . "_" . $dbModuleName . "_" . $dbModuleId; 
    if($ov == $_POST["module"]) 
     echo "<option selected='selected' value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL; 
     $selectedText = $dbModuleNo .' - '. $dbModuleName; 
    else 
     echo "<option value='$ov'>$dbModuleNo - $dbModuleName</option>" . PHP_EOL; 
} 
?> 

再後來......

<strong>Selected Module:</strong><?php echo $selectedText;?>