【问题标题】:place the results of a pg_query into a select dropdown box将 pg_query 的结果放入选择下拉框中
【发布时间】:2018-07-17 12:54:56
【问题描述】:

我正在运行一个 pg_query,它显示我创建的用户表的 uid 列中的所有数据。 我需要的是将结果显示在我创建的用户名选择选择下拉框中,而不是在 html 页面上。我的代码如下:

<?php 
    include_once 'newheader.php' ;
 ?>
 <!-- css for the page-->
 <section class="main-container">
    <div class="main-wrapper">
        <h2 class="page_title">New User</h2>
    </div>
<!-- dropdown list for all user names-->    
     <p>
What is your User Name?
<select name="Usernameselect">
  <option value="">Select...</option>
  <option value="uid">'$row[0]'</option>  
</select>
</p>
  </section>  
<?php
// connect to database
$conn = pg_pconnect("host=localhost dbname=vcbv2 user=postgres");
if (!$conn) {
  echo "An error occurred.\n";
  exit;
}
// get all the uid from the uid column in users
$result = pg_query($conn, "SELECT uid FROM users");
if (!$result) {
  // error message  
  echo "An error occurred.\n";
  exit;
}
// dispaly on screen all uid data from users
while ($row = pg_fetch_row($result)) {
  echo "$row[0]";
  echo "<br />\n";
}
?>

任何帮助将不胜感激。

【问题讨论】:

  • 那么您必须将处理结果集的代码放在您当前拥有该下拉列表的&lt;option&gt; 标记的位置。 显然,在将查询结果实际读入 $row 之前,您无法访问 $row
  • 谢谢 RiggsFolly 我已经将 现在放在脚本的末尾,但它已将所有数据放在 1 行,我需要什么让用户名在不同的行上?
  • 所有数据到一行?对不起,我不明白
  • 当我点击下拉菜单而不是 1 行说 Dave next line John 等时,它会显示所有一行,即 Dave John
  • 我认为这最好作为另一个问题提出

标签: php html postgresql psql


【解决方案1】:
<?php 
    include_once 'newheader.php' ;
 ?>
 <!-- css for the page-->
 <section class="main-container">
    <div class="main-wrapper">
        <h2 class="page_title">New User</h2>
    </div>
<!-- dropdown list for all user names-->    
     <p>
What is your User Name?
<select name="Usernameselect">
  <option value="">Select...</option>
<?php
// connect to database
$conn = pg_pconnect("host=localhost dbname=vcbv2 user=postgres");
if (!$conn) {
  echo "An error occurred.\n";
  exit;
}
// get all the uid from the uid column in users
$result = pg_query($conn, "SELECT uid,name FROM users");
if (!$result) {
  // error message  
  echo "An error occurred.\n";
  exit;
}
// dispaly on screen all uid data from users
while ($row = pg_fetch_row($result)) {
  echo '<option value="'.$row[0].'">'.$row[1].'</option>';
}
?>
</select>
</p>
</section>  

【讨论】:

    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多