gpt4 book ai didi

php - 使用功能选择不显示的选项

转载 作者:行者123 更新时间:2023-11-29 10:16:04 25 4
gpt4 key购买 nike

我正在尝试在 PHP 中动态显示选择选项的值。基本上,用户创建那些存储在数据库中的选项,然后我尝试在不同的地方获取它。为此,我编写了以下函数:

function fetch_acad_yr($conn) { 
$query = "SELECT a.fk_acadyear_id as acadyearid,b.acad_year as acadyear FROM tbl_admparam a inner join list_acad_years b on a.fk_acadyear_id = b.pk_acad_year_id";
$stmt = $conn->prepare($query);
if ($stmt->execute()) {
foreach ($stmt as $row) {
?>
<option value="<?php echo $row['acadyearid'] ?>"><?php echo $row['acadyear'] ?></option>
<?php
}
}
}

然后在 html 中我调用了这个函数

<select class="form-control" id="acad_period" name="acad_period" required>
<option value="">Please select...</option>
<?php fetch_acad_yr($conn); ?>
</select>

但选项不显示任何值,它只是空白,并且不显示存储在数据库中的值。我尝试单独运行查询,查询返回所需的结果

最佳答案

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$con = new mysqli($servername, $username, $password,$dbname);
if ($con->connect_error)
{
die("Connection failed: " . $con->connect_error);
}

?>
<select name="acad_period" required="">
<option value="0">Select</option>
<?php
$s= "SELECT a.fk_acadyear_id as acadyearid,b.acad_year as acadyear FROM tbl_admparam a inner join list_acad_years b on a.fk_acadyear_id = b.pk_acad_year_id";
$ex= $con->query($s);
if (!$ex)
{
echo("Error description: " . mysqli_error($con));
}
else
{
while ($f= $ex->fetch_assoc())
{
?>
<option value="<?php echo $f['acadyearid'] ?>"><?php echo $f['acadyear'] ?></option>
<?php
# code...
}
}

?>
</select>

关于php - 使用功能选择不显示的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50098665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com