沃梦达 / 编程问答 / php问题 / 正文

PHP- 从数据库中获取并存储在下拉菜单 html 中

PHP- Fetch from database and store in drop down menu html(PHP- 从数据库中获取并存储在下拉菜单 html 中)

本文介绍了PHP- 从数据库中获取并存储在下拉菜单 html 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法获得以下代码来制作包含来自 mysql 数据库的数据的下拉菜单.包含('connect.php');"连接到 mysql 数据库,我知道它在不同的页面上工作.有什么建议?

I can't seem to get the following code to make a dropdown menu that contains data from a mysql database. The "include('connect.php');" connects to the mysql database and I know it works on separate pages. Any suggestions?

以下是完整代码.列出客户

Below is the entire code. listCustomer

 <BODY>
 <H1>Find Customer's Albums Page</H1>
 From a dropdown list of customers, a user should be able to pick a customer and see a list of     albums (all fields in the CD table) purchased by that customer.
 <HR>
 <FORM ACTION="listCustomer.php" METHOD="POST"/>
 Customer:
 <select name="mydropdownCust">
 <option value="101">101</option>
 <option value="102">102</option>
 <option value="103">103</option>
 <option value="104">104</option>
 <option value="105">105</option>
 <option value="106">106</option>
 <option value="107">107</option>
 <option value="108">108</option>
 <option value="109">109</option>
 <option value="110">110</option>
 </select>
 <BR>

 <?php
 include('connect.php');

 $query = "SELECT Cnum, CName FROM Customer";
 $result = mysql_query ($query);
 echo "<select name=dropdown value=''>Dropdown</option>";
 while($r = mysql_fetch_array($result))
 {
 echo "<option value=$r["Cnum"]>$r["CName"]</option>"; 
 }
 echo "</select>";
 ?>

 <BR>
 <INPUT TYPE="SUBMIT" Value="Submit"/>
 </FORM>

 <FORM ACTION="listMenu.html" METHOD="POST"/>
 <INPUT TYPE="SUBMIT" Value="Main Menu"/>
 </FORM>
 </BODY>
 </HTML>

推荐答案

<?php
include('connect.php');

$query = "SELECT Cnum, CName FROM Customer";
$result = mysql_query ($query);
echo "<select name='dropdown' value=''><option>Dropdown</option>";
while($r = mysql_fetch_array($result)) {
  echo "<option value=".$r['Cnum'].">".$r['CName']."</option>"; 
}
echo "</select>";
?>

从表面上看,您缺少一个开始选项标签,所以它只是输出Dropdown"作为一行文本.

From the looks of things, you're missing an opening option tag, so it's just outputting "Dropdown" as a line of text.

为了完全透明,因为我没有connect.php,我不得不添加我自己的数据库连接.我的整个页面看起来如此:

Just to be completely transparent, because I did not have connect.php, I had to add my own DB connections. My whole page looked thusly:

<?
//Adding to display errors.
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<HTML>
<HEAD>
</HEAD>
<BODY>
 <H1>Find Customer's Albums Page</H1>
 From a dropdown list of customers, a user should be able to pick a customer and see a list of albums (all fields in the CD table) purchased by that customer.
 <HR>
 <FORM ACTION="listCustomer.php" METHOD="POST"/>
 Customer:
 <select name="mydropdownCust">
 <option value="101">101</option>
 <option value="102">102</option>
 <option value="103">103</option>
 <option value="104">104</option>
 <option value="105">105</option>
 <option value="106">106</option>
 <option value="107">107</option>
 <option value="108">108</option>
 <option value="109">109</option>
 <option value="110">110</option>
 </select>
 <BR />
 <?php
  // BEGIN ADDED CONNECTION HACKY GARBAGE
  $con=mysql_connect("localhost","root","root");
  // Check connection
  if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $selected = mysql_select_db("sample",$con) 
    or die("Could not select examples");
  // END ADDED CONNECTION HACKY GARBAGE

  $query = "SELECT Cnum, CName FROM Customer";
  $result = mysql_query ($query);
  echo "<select name='dropdown' value=''><option>Dropdown</option>";
  while($r = mysql_fetch_array($result)) {
    echo "<option value=".$r['Cnum'].">".$r['CName']."</option>"; 
  }
  echo "</select>";
 ?>

 <BR />
 <INPUT TYPE="SUBMIT" Value="Submit"/>
 </FORM>

<FORM ACTION="listMenu.html" METHOD="POST"/>
<INPUT TYPE="SUBMIT" Value="Main Menu"/>
</FORM>
</BODY>
</HTML>

这篇关于PHP- 从数据库中获取并存储在下拉菜单 html 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP- 从数据库中获取并存储在下拉菜单 html 中

基础教程推荐