我试图简单地显示表中的两个字段,例如firstname、lastname,将它们组合并显示在可以选择的下拉菜单中,并与用户输入的其他数据一起存储。下面,适用于一个字段,但我很难组合姓氏,我尝试过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
试试这个方法:-
$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
要么依赖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>";
}
但不要两者兼而有之。