[英]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.