我想在按下提交后将值插入数据库后,选择下拉选项保持选中状态。我的代码正在运行,但事实并非如此。我目前有两种选择,美国和炸鸡,这些都是美食。当我点击American时,American会被提交到DB中,但Fried Chicken选项会在页面上保持选中状态,反之亦然。
我的下拉菜单中的信息是从我的数据库动态生成的。我哪里出错了。
$get_cats = "SELECT * FROM Rest_Category";
$run_cats = mysqli_query($dbc, $get_cats);
while ($row_cats = mysqli_fetch_array($run_cats)) {
$CategoryID = $row_cats['CategoryID'];
$Cuisine_category = $row_cats['Cuisine_category'];
$selected = $Cuisine_category ? 'selected' : '';
echo"<option ".$selected." value=' $CategoryID'>$Cuisine_category </option>";
}
表格结构
Rest_Category
+-----------+------------+-----------+--------+
| CategoryID | Cuisine_category | Category_img |
+-----------+------------+-----------+---------+
| 1 | American | img |
| 2 | Fried Chicken | img |
+-----------+-------------------+--------------+
休息详情
+-----------+------------+-----------+--------+
| Restaurant_ID | Restaurant_name | Cat_ID |
+-----------+------------+-----------+---------+
| 1 | McDonalds | 1 |
| 2 | KFC | 1 |
+-----------+-------------------+--------------+