我有一张表格,表格标题如下。该表的每一行都填充来自数据库的信息。其中一列(类别)是一个下拉菜单(有很多条目),所以我使用了一个单独的表来从中提取值。当用户提交选择了下拉值的页面时,我希望他们看到他们上次选择的值,因为他们可能必须多次返回此表单。如果您查看 var_dump 的位置,我可以看到它正在从数据库中提取先前选择的值,但是当我尝试在 selected = $row["category" 中使用相同的 $row["category"] 条目时] 我得到下拉列表中的第一个条目,而不是选定的值(再次通过 var_dump 验证为存在)。我不知道为什么会发生这种情况,并且在询问社区之前花了 DAYS 天试图解决这个问题。感谢您的任何帮助,您可以提供。
<body>
<table>
<tr>
<th scope="col">Date</th>
<th scope="col">Amount</a></th>
<th scope="col">Vendor</th>
<th scope="col">Description</th>
<th scope="col">Category</th>
<th scope="col">Notes</th>
<th scope="col">Bind Values?</th>
<th scope="col">Trip</th>
<th scope="col">Last Updated</th>
<th scope="col">Updated By</th>
<th scope="col">Done</th>
</tr>
<?php
$category_query = "SELECT * FROM category order by category_name ASC";
$category_result = mysqli_query($connection, $category_query);
$category_options = NULL;
while ($category = mysqli_fetch_assoc($category_result)) {
$category_options .= sprintf("<option value='%s'>%s</option>".PHP_EOL, $category["category_name"], $category["category_name"]);
}
?>
<form action="process_table.php" method="post">
<?php
$transactions_query = "SELECT transactions_id, `date`, amount, vendor, description, category , trip, last_updated, last_updated_by, done, notes\n"
. "FROM users, transactions\n"
. "WHERE users.user_name = '{$_SESSION["user"]}' AND users.users_id = transactions.users_id";
$transactions_result = mysqli_query($connection, $transactions_query);
while ($row = mysqli_fetch_assoc($transactions_result)) {
$id = $row["transactions_id"];
var_dump($row["category"]);
?>
<tr>
<td><?php echo date("F d, Y", strtotime($row["date"]));?></td>
<td><?php echo number_format($row["amount"], 2);?></td>
<td><?php echo $row["vendor"];?></td>
<td><?php echo $row["description"];?></td>
<td> <?php
if ($row["done"] == "checked") {
echo '<select selected ="'.$row["category"].'" name="expense['.$id.'][category]">'.$category_options.'</select>';
} else {
echo "DUMB"/*'<select name="expense['.$id.'][category]">'.$category_options.'</select>'*/;
}
?>
</td>
<td><?php echo "<textarea name='expense[" . $id . "][notes]'>" . $row["notes"] . "</textarea>";?></td>
<td><input name="bind" type="checkbox" value="YES"></td>
<td nowrap="nowrap">Trip place holder</td>
<td><?php echo date("m/d/Y")?></td>
<td>
<?php
if (!isset($_SESSION["user"])) {
echo "temp" ;
} else {
echo $_SESSION["user"];
}
?>
</td>
<td><?php echo "<input name='expense[" . $id . "][done]' type='checkbox'"?> <?php if($row["done"] == "checked") {
echo "checked = 'checked'>";
} else {
echo ">";
}
?>
</td>
</tr>
<?php
}
?>
</table>
<input name="submit" type="submit" id="submit" >
</form>
</body>