如何自动选择数据库中的保存值以选择HTML标记 [英] How to auto selected save value from database in to select HTML tag

查看:63
本文介绍了如何自动选择数据库中的保存值以选择HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自动选择从数据库中获取的保存值以选择HTML标记.

How to auto select the save value fetched from database in to select HTML tag.

我在一个表中列出了带有酒店名称,房间类型,设施,描述的房间.我想编辑该记录.当我单击编辑"按钮时,它会提取room_id进行相应的编辑,除选择标记中的值外,所有其他值均已成功自动在文本框中自动提取.

I have a list of rooms with Hotel name , room type , facilities , descriptions in a table. i want to edit that record . when i click on edit button it fetch room_id to edit row accordingly, all other values successfully auto fetched in textboxes except values in select tag.

这是我的代码,用于从数据库中获取值并回显到相应的文本框(选择框除外).我想在选择框中自动选择值.

Here is my code to fetch values from database and echo to corresponding textboxes , except select box . i would like to auto select value in select box.

$query = "SELECT * from room where room_id = '$id'";

$result = mysqli_query($connection, $query) or die ( mysqli_error());
 while($row=mysqli_fetch_assoc($result))
  { 
       $room_id=$row['room_id'];
        $room_type_id=$row['room_type_id'];
        $facilities = $row['facilities'];
        $long_description=$row['long_description'];
}
         ?>

                  <form action="Edit_Room_Script.php" method="post"  enctype="multipart/form-data">


                <label class="form-label">Hotel Name</label> 
<select class="form-control" name="name" id="name">
<?php
$sel_cus = "select hotel_name from hotels ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['hotel_id'];?>"><?php echo $row['hotel_name'];?></option><?php}?>
</select>

<label class="form-label">Room Type</label> 
<select class="form-control" name="room_type" id="room_type">
<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>">
<?php echo $row['Room_Type_Name'];?></option>
<?php
} 
?>
</select>
<label class="form-label">Facilities</label> 
<input class="form-control" type="text" id="facilities" value="<?php echo $facilities;?>" name="facilities" autocomplete="Off"  required >

<button class="btn btn-success btn-cons" type="submit" name="update" id="update"> Update Room</button>
</form>

我的html表中有一行,如下所示

i have a row in my html table is like below

高级酒店名称房型设施动作

Sr Hotel Name Roomtype Facility Action

1个Serena超级ABC编辑

1 Serena Super ABC Edit

当我单击编辑"按钮时,我需要从数据库中自动成功获取设施值并在文本框中进行设置的位置进行编辑,但未设置酒店名称和房型.在用于酒店名称和房型的选择标签中,它会填充除serena和super之外的所有酒店名称和房型.我该如何做到这一点,请提供一些代码. 谢谢

When i click to edit button it take me to edit from where facility value successfully auto fetched from database and set in text box but hotel name and room type is not set. In select tag for hotel name and room type it populates all the hotel name and room type except serena and super how could i achieve this Please guide with some code. Thank you

推荐答案

一些修改:

1)获取$hotel_id

2)要显示选择的<select>元素,您需要在<option>标记中添加selected="selected"属性.

2) To show <select> element selected, you need to add selected="selected" attribute in your <option> tag.

如果您有下拉HTML:

If you have a drop down HTML:

<select class="form-control" name="room_type" id="room_type">
<option value="1">Villa</option>
<option value="2">Terrace</option>
<option value="3" selected="selected">Gallery</option>
<option value="4">Delux</option>
</select>

下拉菜单将显示Gallery已选择.

Drop down will show Gallery selected.

在您的情况下,您可以通过以下方式显示它:

In your case, you can show it by:

<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>"
<?php if ($room_type_id == $row['Room_Type_Id']) { echo 'selected="selected"';}?>>
<?php echo $row['Room_Type_Name'];?></option>
<?php
} 
?>

3)另外,首先从数据库中获取数组,然后在<select>元素中对其进行循环,因为直接在<select>中编写获取代码是一种不好的做法,如果出现任何问题,它会暴露您的数据库字段名称.

3) Also, first fetch the arrays from database and then loop over them in the <select> element as writing fetching code directly in <select> is a bad practise and in case of any issue, it my expose your database field names.

这篇关于如何自动选择数据库中的保存值以选择HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆