简体   繁体   中英

HTML/PHP Form Select Option with link to MySQL

I followed a previous question on here to get a drop down with link to MySQL. It works without the form but with the form, all the room_id 's just scatter across one line and don't go in a drop down box. Any ideas on how to fix it? Thank you

//Creates a form for room_id
echo "<form action=''>";
echo "<select name='room_id'>";
//Creates drop down box to show the current rooms vacant
$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
echo "<select name='room_id'>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select>";

//Submit button
echo "<input type='submit' value='Submit'>";

//Closes form
echo "</form>";

The reason is that you are echoing your select twice. Remove one.

//Creates a form for room_id
echo "<form action=''>";
echo "<select name='room_id'>";
//Creates drop down box to show the current rooms vacant
$sql = "SELECT * FROM room";
$sql.= " WHERE room_vacant = 1";
$stmt = $dbh->query($sql);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<option value='" . $row['room_id'] . "'>" . $row['room_id'] . "</option>";
} //Closes drop down box
echo "</select>";

//Submit button
echo "<input type='submit' value='Submit'>";

//Closes form
echo "</form>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM