如何解决使用程序准备好的语句填充我的下拉菜单
我想问一下如何通过在 MysqL 数据库中检索我的数据来填充我的下拉列表/<select>
。我正在使用程序准备语句来添加安全性或避免 sql 注入。
问题:它只从我的数据库中检索一个数据,即我的表中确实存储了两个数据,如何通过我的 <option>
标签插入它?我目前正在做的是首先检索所有 research_title
。
index.PHP
<form action="#" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="LabelTitle">Research Title</label>
<?PHP
include 'includes/includes_getoptionList.PHP';
?>
</div>
<?PHP
// Connection in DB is stored here
include 'connection_operation.PHP';
$query = "SELECT research_title FROM tbl_topicresearch";
$smtmt = MysqLi_stmt_init($conn);
if (!MysqLi_stmt_prepare($smtmt,$query)) {
echo "sql Statement" . MysqLi_stmt_error($smtmt);
} else {
MysqLi_stmt_execute($smtmt);
$getResult = MysqLi_stmt_get_result($smtmt);
if ($getResult) {
?>
<select class="form-control" name="research_title" id="research_title">
<?PHP
while ($row = MysqLi_fetch_assoc($getResult)) {
$title_research = $row['research_title'];
echo $title_research;
echo '<option value="<?PHP echo $row[research_title]; ?>"> <?PHP echo $row[research_title]; ?> </option>';
?>
</select>
<?PHP
}
} else {
}
}
?>
解决方法
-
</select>
结束标记应该在while
循环之外。 - 不得在 PHP 中包含 PHP(例如:
echo '<?php ... ?>';
)。 - 您有一个额外的电话 (
echo $title_research;
)。
代码:
if ($getResult)
{
echo '<select class="form-control" name="research_title" id="research_title">';
while ($row = mysqli_fetch_assoc($getResult))
{
$title_research = $row['research_title'];
echo '<option value="' . $title_research . '">' . $title_research. '</option>';
}
echo '</select>';
}
else
{
echo '<p>no results</p>';
}
或者:
<?php if ($getResult) : ?>
<select class="form-control" name="research_title" id="research_title">
<?php while ($row = mysqli_fetch_assoc($getResult)): ?>
<option value="<?php echo $row['research_title'] ?>">
<?php $row['research_title'] ?>
</option>
<?php endwhile; ?>
</select>
<?php else: ?>
<p>no results</p>
<?php endif; ?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。