3

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

下面是整个代码。列出客户

 <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>
4

4 回答 4

5
<?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”作为一行文本输出。

编辑

只是为了完全透明,因为我没有connect.php,我必须添加自己的数据库连接。我的整个页面看起来是这样的:

<?
//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>
于 2013-05-22T21:38:26.977 回答
4

首先,您缺少一个选项开始标签,正如 stslavik 正确提到的那样。但这并没有引起这里看起来的问题(它是由浏览器自动更正的——至少在我的测试中)。

其次,这不起作用(问题原因):

echo "<option value=$r["Cnum"]>$r["CName"]</option>"; 

你应该使用

echo "<option value=".$r["Cnum"].">".$r["CName"]."</option>"; 

或者,因为我总是喜欢用单引号括住 echo 或打印输出字符串:

echo '<option value='.$r['Cnum'].'>'.$r['CName'].'</option>';

第三种选择(复杂的语法:PHP 语法中的 ${ } 是什么意思?

echo "<option value={$r["Cnum"]}>{$r["CName"]}</option>";
于 2013-05-22T21:52:35.913 回答
1

假设你从数据库中获取数据试试这个

echo "<option value={$r['Cnum']}>{$r['CName']}</option>"; 
于 2013-05-22T21:36:34.270 回答
-1

尝试,

echo "<option value=' . $r['Cnum'] . '>' . $r['CName'] . '</option>"; 

代替

echo "<option value=$r[Cnum]>$r[CName]</option>";
于 2013-05-22T21:19:39.050 回答