使用来自 SQL 的 table 数据作为 select 输入中的选项
Using table data from SQL as options in select input
我有一个名为“Registration”的 table 用于注册俱乐部,其中包含以下列 'clubName' 和 'registrationStatus' clubName 包含俱乐部的名称,registrationStatus 包含这三列之一,打开、关闭或扩展。在我的 HTML 表单中,我有 select 输入。在我想从数据库俱乐部名称中获得的选项中,俱乐部名称的状态仅为打开和扩展。或者所有俱乐部的名称,但状态为关闭的俱乐部将不会激活。在我的代码中,我有以下内容
<?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>
您需要先 运行 查询,然后使用关联数组循环遍历值以获取值。这些值然后可以显示在您的下拉菜单中。请务必在收集值时过滤您的值以防止 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>
我有一个名为“Registration”的 table 用于注册俱乐部,其中包含以下列 'clubName' 和 'registrationStatus' clubName 包含俱乐部的名称,registrationStatus 包含这三列之一,打开、关闭或扩展。在我的 HTML 表单中,我有 select 输入。在我想从数据库俱乐部名称中获得的选项中,俱乐部名称的状态仅为打开和扩展。或者所有俱乐部的名称,但状态为关闭的俱乐部将不会激活。在我的代码中,我有以下内容
<?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>
您需要先 运行 查询,然后使用关联数组循环遍历值以获取值。这些值然后可以显示在您的下拉菜单中。请务必在收集值时过滤您的值以防止 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>