繁体   English   中英

来自数据库的 Select ID 作为选项值在 php 中不起作用

[英]Select ID from database as option value is not working in php

我正在尝试构建一个数据更新程序。 但是来自数据库的 select ID 作为选项值在 php 中不起作用。 来自 db 的数据也无法在 textarea 中显示。 我访问了 Stackoverflow 中发现的两个几乎相似的问题,但无法解决我的问题。 请有人指导我。 我的代码如下。 除了该文本框显示印度文本的编码问题。[当前状态的屏幕截图]

    <?php
include_once 'inclues/connection_string.php';
$id="7";
$Eng_Word="";
$As_Meaning="";
$New_Meaning="";
?>
<!doctype html>

<div class ="container">
    <div class="card p-3 bg-secondary">
        <form action = "update.php" method ="post">
            <div class="form-row">
                <div class="col-sm-2">
                <label for="ID">SlNo</label>
                <select class="form-control" id="sel1">
                <option value="">Sl no</option>
                <?php
                    $stmt = "SELECT id FROM bengali";
                    $stmt= $conn->prepare($stmt);
                    $stmt->execute();
                    while ($row = $stmt->fetch())
                    {
                    echo '<option value=' .$row['id'] . '>' . '</option>';
                    }
                ?>
                </select>
                </div>
            </div>
            <?php
                if (isset($_POST['id']))
                    {
                    $id= $_POST['id'];
                    }
                else
                {
                    echo "Please select ID";
                }
                $stmt = "SELECT * FROM bengali WHERE id=$id";
                $stmt= $conn->prepare($stmt);
                $stmt->execute();
                $row=$stmt->fetch();
                $Eng_Word = $row['Eng_Word'];
                $As_Meaning = $row['As_Meaning'];
                $Bn_Meaning = $row['Bn_Meaning'];
            ?>
            <div class="form-row">
                <div class="col-sm-4">
                    <label for="Eng_Word">English Word</label>
                    <input type="text"  value="<?php echo $Eng_Word;?>" class="form-control">
                </div>
            </div>
          <div class="form-group">
            <label for="As_textarea">Assamese Meaning</label>
            <input type="text"  value="<?php echo $As_Meaning;?>" class="form-control">
            <hr>
            <hr>
            <textarea class="form-control" name="As_Meaning" id="AssameseMeaning" value="<?php echo $As_Meaning;?>" rows="3"></textarea>
          </div>
          <div class="form-group">
            <label for="Newtextarea">New Meaning</label>
            <textarea class="form-control" name="Bn_Meaning" id="BengaliMeaning" value="<?php echo $Bn_Meaning;?>"  rows="3"></textarea>
          </div>
          <div>
          <button type="button" class="btn btn-primary btn-lg btn-right">Save Now!</button>
          </div>
        </form>
    </div>
</div>

execute方法之后,您应该存储结果并将其绑定到变量

<?php
    $stmt = "SELECT id FROM bengali";
    $stmt= $conn->prepare( $stmt );
    $stmt->execute();

    $stmt->store_result();
    $std->bind_result( $id );

    while ( $row = $stmt->fetch() ) {
        printf( '<option value="%1$d">%1$d', $id );
    }
?>

替换此代码

echo '<option value=' .$row['id'] . '>' . '</option>';

echo "<option value='$row[id]'>$row[id]</option>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM