0

我试图简单地显示表中的两个字段,例如名字、姓氏,将它们组合并显示在可以选择的下拉菜单中,并与用户输入的其他数据一起存储。下面,适用于一个领域,但我正在努力组合姓氏,我尝试过 concat 但我认为我做错了。提前致谢。

//Drop Down Select
  $sql = "SELECT concat (firstname, lastname) as username  FROM users_tbl";
$result = pg_query($sql);
echo "<select name='firstname'>";

while ($row = pg_fetch_array($result)) {
 echo "<option value='" . $row['firstname'] . "'>" . $row['firstname'] ."</option>";

}
echo "</select>";
// close connection
4

2 回答 2

1

要么依赖SQL...

while ($row = pg_fetch_array($result)) {
    echo "<option value='" . $row['username'] . "'</option>";
}

或者不要在 SQL 中使用 CONCAT,而是使用 PHP:

while ($row = pg_fetch_array($result)) {
    echo "<option value='" . $row['firstname'] . " " . $row['lastname'] ."'</option>";
}

但不要两者都做。

于 2013-06-24T09:58:40.397 回答
1

试试这种方式: -

$sql = "SELECT concat (firstname, lastname) as username  FROM users_tbl";
$result = pg_query($sql);
echo "<select name='firstname'>";

while ($row = pg_fetch_array($result)) {
echo '<option value="'.$row['username'].'" >'.$row['username'].'</option>';
}
echo "</select>";
// close connection
于 2013-06-24T10:02:24.637 回答