1

我正在尝试将从 mysql 中提取的值与从 mysql 中提取的数组进行比较。代码如下所示:

$aeSelect = "SELECT id, firstName, lastName from user WHERE fiID = 0 AND access = 3 ORDER BY lastName";
$aeQuery = mysql_query($aeSelect) or die ("Could not get AE list: " . mysql_error());
while ($rowAE = mysql_fetch_array($aeQuery)) {
   echo "<option " . (($row['aeid'] == $rowAE['id']) ? "selected='selected'" : "") . ">" . $rowAE['firstName'] . " " . $rowAE['lastName'] . "</option>";
}

$row['aeid']是 20022,$rowAE['id']是 AE 的 id 列表。

我无法正确执行此操作,因为选择菜单显示了 AE 的所有名称,但没有选择正确的名称。为什么?我究竟做错了什么?

4

2 回答 2

0

首先,我只想说,在这个时代,除非这是一个非常小的站点,否则不使用 Yii 或 Cake 这样的框架在 PHP 中进行 DB 访问有点愚蠢。

话虽如此,您遇到的问题是一个简单的解决方法:

$aeSelect = "SELECT id, firstName, lastName from user WHERE fiID = 0 AND access = 3 ORDER BY lastName";
$aeQuery = mysql_query($aeSelect) or die ("Could not get AE list: " . mysql_error());
while ($rowAE = mysql_fetch_array($aeQuery)) {
   echo "<option " . (($row['aeid'] == $rowAE['id']) ? "selected='selected'" : "") . ' value="'. $rowAE['id']. "\">" . $rowAE['firstName'] . " " . $rowAE['lastName'] . "</option>";
}
于 2013-10-15T18:33:12.087 回答
0

将选项行更改为:

echo "<option ";
if ($row['aeid'] == $rowAE['id']) echo 'selected="selected"';
echo  ' value="'.$rowAE['id'].'">' . $rowAE['firstName'] . ' '. $rowAE['lastName'] . '</option>';

我希望它是好的:)

于 2013-10-15T18:43:52.117 回答