php - 从数据库中选择 ID,因为选项值在 php 中不起作用
问题描述
我正在尝试构建一个数据更新程序。但是从数据库中选择 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 );
}
?>
推荐阅读
- swiftui - 让用户将多个图像添加到 SwiftUI 视图
- sharepoint-online - 如何使用 Power Automate 处理刷新/替换的 excel 文件
- flutter - Flutter 何时重建(重新创建)小部件?有状态的?无国籍?它会持续存在吗?
- laravel - 为什么我在写入会话时突然开始收到来自服务器的 502 响应?
- python - 连接到 Binance websocket 时出错
- android - Android Retrofit 的 @Path 不起作用。为什么?
- mysql - sql只选择重复的行
- .net-5 - 如何在 .Net Core 中创建用户可管理的业务规则
- html - 输入和按钮未水平对齐
- javascript - 在 nunjucks 循环中构建数组/对象