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 } ?>
就問 - 什麼'WHERE ModuleActive =? '聲明的意思? – Kamo
@Kamo?是一個參數。參數存儲在這裏:'$ sqlstmt-> bind_param(「i」,$ moduleactive); '和'$ moduleactive = 1;'。所以WHERE語句是'WHERE ModuleActive = 1' – user1964964