如何设置选择的值标签< select> HTML从数据库中的PHP? [英] how to set the selected value tag <select> html from database in php?
问题描述
我正在尝试创建一个下拉菜单,该菜单将选择一个存储在数据库中的值.这是代码:
I'm trying to create a drop down menu that will select a value that is stored in the database. here's the code :
require 'koneksi.php';
$sql_select = "SELECT * FROM supplier";
$hasil = mysql_query($sql_select);
if(!$hasil) {
echo "data supplier not found ".mysql_error();
}
$id = $_GET["id"];
$hasil2 = mysql_query("SELECT * FROM brg_supplier WHERE id_brg=".$id);
$data = mysql_fetch_array($hasil2);
if($hasil2) {
$supplier = $data['nama_supplier'];
}
<select name="supplier">
<option value="">---pilih supplier---</option>
<?php
while($baris = mysql_fetch_array($hasil)){
?>
<option value="<?php $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo $baris['nama_supplier']; ?> </option>;
<?php }?>
</select>
问题是我的代码创建了一个未选择任何内容的下拉列表.这是屏幕截图:链接
the problem is my code creates a dropdown with nothing selected. here's the screenshot : link
我已经尝试了stackoverflow中的所有解决方案.但是下拉列表中的值仍然没有选择.我知道这很简单,我很想念,但严重的是我无法弄清楚.请任何人帮助,谢谢!
i've tried all the solutions in the stackoverflow. but the dropdown value still nothing selected. i know that it has to be something simple that i am missing but seriously i cannot figure it out. please anyone help, thanks!
推荐答案
我认为问题出在这一行:
I think the problem lies in this line:
<option value="<?php $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo $baris['nama_supplier']; ?> </option>;
您错过了回声,看起来很有趣:/ 尝试尝试:
You're missing an echo and it looks funny :/ Try instead:
<option <?php $val=$baris['nama_supplier']; echo "value='$val'"; if($supplier==$val) echo "selected='selected'>";echo $val;?> </option>;
这篇关于如何设置选择的值标签< select> HTML从数据库中的PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!