簡體   English   中英

使用程序准備語句填充我的下拉菜單

[英]populating my dropdown menu using procedural prepared statement

我想問一下如何通過在 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>

包括_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; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM