使用程序准备语句填充我的下拉菜单
populating my dropdown menu using procedural prepared statement
我想问一下如何通过在 MySQL 数据库中检索数据来填充下拉菜单/<select>
。我正在使用程序准备语句来添加安全性或避免 SQL 注入。
问题:它只从我的数据库中检索一个数据,我确实有两个数据存储在我的 table 中,我如何通过我的 <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>
includes_getOptionList.php
<?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; ?>
我想问一下如何通过在 MySQL 数据库中检索数据来填充下拉菜单/<select>
。我正在使用程序准备语句来添加安全性或避免 SQL 注入。
问题:它只从我的数据库中检索一个数据,我确实有两个数据存储在我的 table 中,我如何通过我的 <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>
includes_getOptionList.php
<?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; ?>