我已连接到数据库。
创建了一个下拉框。哪个工作正常:
$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 - PHP SQL集成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43058520/