【问题标题】:Using table data from SQL as options in select input [duplicate]使用 SQL 中的表数据作为选择输入中的选项 [重复]
【发布时间】:2021-08-22 23:23:01
【问题描述】:

我有一个名为“Registration”的表格,用于注册俱乐部,其中包含以下列“clubName”和“registrationStatus”,clubName 包含俱乐部的名称,registrationStatus 包含这三者之一,开放、关闭或扩展。在我的 HTML 表单中,我有选择输入。在我希望从数据库中获得的选项中,俱乐部名称的状态仅为开放和扩展。或者所有俱乐部名称,但状态为已关闭的俱乐部将不会处于活动状态。在我的代码中,我有以下内容

<?php 
//connection to database

include "connect.php";
?>




<label for="club">Select club to join</label>
<select name="club" >
<?php
$result = "SELECT clubName FROM Registration WHERE Status= 'Open' OR Status='Extended'";

//I'm stuck here now how to display the options and how will I attach value to them for entry into the database

?>
</select>

【问题讨论】:

  • 循环查询结果,回显包含$tow['clubName']&lt;option&gt;标签
  • 有很多关于从 SQL 查询结果创建下拉菜单的教程。
  • 我已经尝试了很多但仍然无法获得它
  • 如果你刚刚开始学习 PHP,那么你应该学习 PDO 而不是 mysqli。 PDO 更容易,更适合初学者。从这里开始phpdelusions.net/pdo & websitebeaver.com/…

标签: php html mysqli


【解决方案1】:

您需要先运行查询,然后使用关联数组循环遍历值以获取值。然后可以在下拉菜单中显示这些值。请务必在收集值时过滤它们以防止 XSS 攻击。

<label for="club">Select club to join</label>
<select name="club" >
<?php
$result = "SELECT clubName FROM Registration WHERE Status= 'Open' OR Status='Extended'";
// be sure do define $connection to be your database connection, or just change it in this code to match your configuration settings
$runquery = mysqli_query($connection,$result);
while($row = mysqli_fetch_assoc($runquery)){
  // this will cycle through the array
  $val = $row['clubName'];
  echo '<option value="' . $val . '">' . $val . '</option>';
}

?>
</select>

【讨论】:

    猜你喜欢
    • 2016-07-23
    • 2017-03-06
    • 2021-01-14
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多