0
我從數據庫(該部分工作)填充下拉列表,當我從下拉列表中選擇一個項目我希望它使用新選擇項目信息並通過我的函數運行它來計算項目的生命週期(每個項目有一個生命週期數年進入數據庫)。當我將item_num硬編碼到查詢中時,它工作正常,但是當我嘗試使用下拉菜單進行選擇時,它不會返回任何內容。顯然,我錯過了一些基本的東西。任何幫助將不勝感激。使用從下拉列表中的選擇來更新數據庫查詢
<?php
session_start();
include_once 'databaseConnect.php';
if (isset($_POST['itemDropDown'])) {
$submittedValue = $_POST['itemDropDown'];
}
$query = $db->query("select life_cycle, entered_into_service from assets,
all_items where assets.type = all_items.type and assets.make =
all_items.make and assets.model = all_items.model and all_items.item_num
='.$submittedValue'");
$userRow=$query->fetchAll();
function dateDifference($date_1, $date_2, $differenceFormat = '%a')
{
$dateTime1 = date_create($date_1);
$dateTime2 = date_create($date_2);
$interval = date_diff($dateTime1, $dateTime2);
return $interval->format($differenceFormat);
}
function daysRemaining($entered, $life)
{
$date_1 = $entered;
$date_2 = date('m/d/Y');
echo (365 * $life) - dateDifference($date_1, $date_2) ." days";
}
function get_items() {
global $db;
$query = 'SELECT item_num, type, make, model FROM all_items
ORDER BY item_num';
$statement = $db->prepare($query);
$statement->execute();
return $statement;
}
$itemsInfo = get_items();
?>
<html>
<head>
<title>Date Difference</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="mainStyle.css">
<script src="modernizr.custom.05819.js"></script>
</head>
<body>
<table>
<tr>
<th>Days remaining in service:</th>
</tr>
<?php foreach($userRow as $value) : ?>
<tr>
<td><?php
daysRemaining($value['entered_into_service'], $value['life_cycle']); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<form action="" name="items" method="post" >
<label>Available Items:</label>
<select id="itemDropDown" name="itemDropDown">
<option selected="selected">Choose an Item</option>
<?php foreach ($itemsInfo as $itemInfo) : ?>
<option value="<?php echo $itemInfo['item_num']; ?>">
<?php echo $itemInfo['item_num']; ?>
<?php echo $itemInfo['type']; ?>
<?php echo $itemInfo['make']; ?>
<?php echo $itemInfo['model']; ?>
</option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
</html>
取出點是否啓用了錯誤報告?什麼'var_dump'顯示? – Script47
您應該使用準備好的查詢,而不是將變量替換爲SQL,以防止SQL注入。 – Barmar
當用戶最初在不提交表單的情況下加載頁面時,'$ submittedValue'將爲空,並且您的查詢將會出現語法錯誤。 – Barmar