我已连接到我的数据库。 创建了一个下拉框。哪个工作正常:
$sql = "SELECT * FROM Customer";
$result = mysql_query($sql);
echo "<b>Customer Name : </b>" . "<select id='CustomerName' name='CustomerName'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['CustomerName'] . "'>" . $row['CustomerName'] . "</option>";
}
现在我无法尝试将表 Customer 中的值放入数组中 看起来像这样:
$types = array (
'A' => 10.99,
'B' => 4.99,
'C' => 13.99);
我打算做的是创建这个数组, 代替 A B C,有 CustomeName 和 CustomerPrice 代替价格。 客户表有
+----------------+-----------------+---------------+
| Customer_ID | CustomerName | CustomerPrice |
+----------------+-----------------+---------------+
| 1 | A | 10.99 |
| 2 | B | 4.99 |
| 3 | C | 13.99 |
+----------------+-----------------+---------------+
请帮忙
最佳答案
除了生成数组外,您基本上做与下拉列表相同的事情。你可以在同一个循环中完成:
$sql = "SELECT * FROM Customer";
$result = mysql_query($sql);
$array = array();
echo "<b>Customer Name : </b>" . "<select id='CustomerName' name='CustomerName'>";
while ($row = mysql_fetch_array($result)) {
$array[$row['CustomerName']] = $row['CustomerPrice'];
echo "<option value='" . $row['CustomerName'] . "'>" . $row['CustomerName'] . "</option>";
}
希望对你有帮助
关于PHP SQL 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43058520/