【问题标题】:Displaying results from a dropdown list显示下拉列表中的结果
【发布时间】:2014-07-04 04:53:15
【问题描述】:

我一直在创建一个连接到另一个 php 页面的下拉列表。我使用 sql 查询列出了人员名称,但我需要它们具有人员 ID 的值。我已连接页面 task7.php(其中有一个查询显示给定人员 ID 的购买信息),因此一旦用户单击名称然后单击提交,应显示该人的订单信息。目前我能够查看下拉列表,选择一个名称,但是当我单击提交时,该表只有一个空表的字段名称。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 9</title>
</head>

<body>

<?php
$conn = mysql_connect("localhost", "twa291", ".......");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );



?>

<form method="get" action="task7.php">
<select name="list" id="list" size="12">

<?php
$sql = "SELECT staffID, staffName FROM staff";
$result = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

while ($row = mysql_fetch_array($result)){
$title=$row["staffName"];
$id=$row["staffID"];

echo "<option value= ".$id.">".$title."</option>";

}
?>
<input type="submit" value="Submit" method="get">
</select>
</form>

<?php 
mysql_close($conn); ?>


</body>
</html>

这是我的 task7.php 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 3</title>
</head>
<body>

<?php
$conn = mysql_connect("localhost", "twa291", "......");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );  ?>

<?php
$staffid= $_GET["staffID"];


?>

<?php

$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase, 
staff 
WHERE staff.staffID='$staffid'"; 


$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

?>

<table border="1" summary="Staff Orders">
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Staff Name</th>
</tr>

<?php
while ($row = mysql_fetch_array($rs)) { ?>

<tr>

<td><?php echo $row["orderID"]?></td>
<td><?php echo $row["orderDate"]?></td>
<td><?php echo $row["shippingDate"]?></td>
<td><?php echo $row["staffName"]?></td>

</tr>


<?php   }
mysql_close($conn); ?>
</table>
</body>
</html>

【问题讨论】:

  • 我不确定这是否会对您有所帮助,但您的 HTML 无效——您的提交按钮位于结束 &lt;/select&gt; 内。修复它,看看它是否有帮助。
  • 不,还有一张空桌子。我被卡住了,它甚至都不好笑:(。就像我做对了一切,但它不起作用。也许我必须使用某种表单名称或 id 或选择 id?嗯
  • 我还需要从 task7.php 文件中调用任何内容吗?有一个 $staffid=$_GET["staffID"] 变量还不够吗? (除了显示购买细节的编码之外)。
  • 试试$staffid = $_GET["list"]
  • 那行不通。但我所做的是更改

标签: php mysql sql forms xhtml


【解决方案1】:

你用" "包裹了不需要的字符串

<?php
$sql = "SELECT staffID, staffName FROM staff";
$result = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

while ($row = mysql_fetch_array($result))
{
$title=$row["staffName"];
$id=$row["staffID"];
echo "<option value= '.$id.'>".$title."</option>"; 

}

?>

【讨论】:

  • 其实,我认为他的意图是“title”是一个变量,而不是文本,所以需要在“title”这个词前添加一个美元符号。
  • 我实际上正要为这两个错误编辑我的代码,抱歉。我仍然在同一行遇到同样的错误
  • 我刚刚添加了两个句号,它就起作用了。但是,我没有收到购买详细信息,只是带有空表的字段名称。这可能是什么原因?谢谢
  • 有什么想法吗?感谢帮助
  • 查看我的代码。在您的代码中仍然看到echo "&lt;option value= ".$id."&gt;title&lt;/option&gt;";
【解决方案2】:
?php
$staffid= $_GET["list"];


?>

<?php

$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase, 
staff 
WHERE staff.staffID='$staffid'"; 

?>

【讨论】:

  • 你可以调试并检查 $_GET['list'] 是否真的返回了一些东西,然后你可以直接在你的数据库 sql 上运行它来查看它是否返回一个表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 2016-04-05
  • 2020-06-28
  • 2019-08-25
  • 2011-09-09
  • 2014-11-12
相关资源
最近更新 更多