PHP/MySQL 下拉列表有多列,语法有问题

发布于 2024-11-09 13:52:47 字数 721 浏览 0 评论 0原文

我正在尝试获取一个下拉列表来显示表中的多个列,并将所选行的主 ID 存储在变量名称中。

如果我删除 CONCAT 函数并选择一列,我会得到一个行列表,但我不知道如何选择多个列。我做错了什么?

<li>
    <?php 
    $sql="SELECT CONCAT(county, ' ',municipality, ' ',park), id FROM mtmg.locality";
    $result=mysql_query($sql, $connection);

    echo '<label for="county_municipality_park">County, Municipality, Park</label>';
    echo '<select  id="county_municipality_park" name="county_municipality_park">';

    while ($row = mysql_fetch_assoc($result)) {echo '<option value="'.$row['county,municipality,park'].'">'.$row['county,municipality,park'].'</option>';}
    echo mysql_error();

    echo '</select>';
    ?>
</li>

I'm trying to get a drop down list to display multiple columns from a table, and for the selected row's primary id to be stored in the variable name.

I get a list of rows if I drop the CONCAT function and SELECT a single column, but I can't figure out how to select more than one. What am I doing wrong?

<li>
    <?php 
    $sql="SELECT CONCAT(county, ' ',municipality, ' ',park), id FROM mtmg.locality";
    $result=mysql_query($sql, $connection);

    echo '<label for="county_municipality_park">County, Municipality, Park</label>';
    echo '<select  id="county_municipality_park" name="county_municipality_park">';

    while ($row = mysql_fetch_assoc($result)) {echo '<option value="'.$row['county,municipality,park'].'">'.$row['county,municipality,park'].'</option>';}
    echo mysql_error();

    echo '</select>';
    ?>
</li>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-11-16 13:52:47

您需要为 CONCAT() 函数指定一个别名,例如

SELECT CONCAT(county, ' ',municipality, ' ',park) as county_municipality_park, id FROM ...

,然后在 $row 数组中引用它,即 $row['county_municipality_park']

You need to give your CONCAT() function an alias, something like

SELECT CONCAT(county, ' ',municipality, ' ',park) as county_municipality_park, id FROM ...

and then reference it as such in the $row array, i.e. $row['county_municipality_park'].

锦欢 2024-11-16 13:52:47

尝试这个

$sql="SELECT CONCAT(county, ' ',municipality, ' ',park) as location , id FROM mtmg.locality";

,然后使用 $row['location']

try this

$sql="SELECT CONCAT(county, ' ',municipality, ' ',park) as location , id FROM mtmg.locality";

and then use $row['location']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文