简体   繁体   English

在选择中显示来自数据库的数据作为选项

[英]Display data from database as option in a select

I'm coding a website for an online DVD rental and booking system and trying to call into a dropdown menu the DVDID which is the primary key and the title of the DVDs in my phpmyadmin data base but when I try to call the ID in it does not work. 我正在为在线DVD出租和预订系统的网站编码,并尝试在下拉菜单中调用DVDID,这是我的phpmyadmin数据库中的主键和DVD的标题,但是当我尝试在其中调用ID时不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

<form name="FrmAmend" method="post" action="amenddvd2.php">
Select from the list below<br>
<select name="Film" title="Select DVD">
    <option value="" selected disabled>Select DVD</option>
<?php

$result = mysqli_query($con, "SELECT `DVDID`, `Title` FROM tbldvd;");
$row = mysqli_fetch_assoc($result);
foreach ($result as $row) {
    echo "<option value='" . $row['DVDID'] . $row['Title'] . "'>" . $row['DVDID'] . $row['Title'] . "</option>";

}
?>
</select><br>
<a href="menu.php"</a>Return To Main Menu<br>
<input type="submit" name="Submit" value="Select DVD">
</a>
</form>

The issue you have is the way you collect the data, you have the following; 您遇到的问题是收集数据的方式,您有以下几点;

$row = mysqli_fetch_assoc($result);
foreach ($result as $row) {

mysqli_fetch_assoc collects a single row from the result set, a better way to do this, would be to replace the above and use the following; mysqli_fetch_assoc从结果集中收集一行,一种更好的方法是替换上面的内容并使用下面的内容;

while ($row = mysqli_fetch_assoc($result) {

And this will get the results out of the database, row by row and print these without having to have changes inside the loop 这样就可以将结果逐行从数据库中取出并打印出来,而不必在循环内进行更改

Looping in a while, using mysqli_fetch_assoc collects each row, one at a time allowing you to use this more effectively. 在一段时间内循环,使用mysqli_fetch_assoc收集每一行,一次一行,使您可以更有效地使用它。
So, if you have the following data; 因此,如果您具有以下数据;

DVDID    Title
1        ABC
2        DEF

A while loop collects the rows, one at a time, for each row in your result set, where a single fetch_assoc will collect the first that appears in the result set and nothing more while循环针对结果集中的每一行一次收集一行,其中单个fetch_assoc将收集结果集中出现的第一行,仅此而已

As an additional, you have the following HTML; 另外,您还具有以下HTML;

<a href="menu.php"</a>Return To Main Menu<br>

This is not valid as HTML, from this, it looks like you want "*Return To Main Menu" to be the hyperlink, so replacing the above with the following will resolve this issue; 这对于HTML无效,由此看来,您希望将“ * Return To Main Menu”作为超链接,因此将上面的内容替换为以下内容将解决此问题;

<a href="menu.php">Return To Main Menu</a><br>

This adds the close to the first "a" ( > ) and moves the text to within the opener and closer tags 这会在第一个“ a”( > )附近添加close,并将文本移至opener和close标签内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Select 选项显示来自数据库的数据 Codeigniter - Display data from database with Select Option Codeigniter 从数据库显示中选择选项到另一个页面 - select option from database display to another page 如何在选择选项中显示/获取数据库中未显示的第2列值? - How to display/get the column 2 value from database not display in select option? 如何在select-option标记中显示mysql数据库中的值 - How to display a value from mysql database in select-option tag 如何使用选项在php中显示数据库中的图像并选择 - How to display images from a database in php using option and select 当我从选择类型中选择“任何”选项并单击提交时,我要显示MySQL数据库中的所有数据 - When I choose 'any' option from select type and click submit, I want to display all the data from MySQL database 从表 A 中选择选项后显示表 B 中的数据 - Display data from table B after select option from table A 如何从数据库中选择数据并将其显示在表中? - How to select a data from database and display it in a table? 从下拉列表中选择“全部”选项时显示数据库中的数据 - display data from database on selecting 'ALL'option from dropdownlist 如何从动态数据下拉列表中显示选择选项 - How to display select option from dynamic data drop down list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM