[英]Import values from database to a select
Good morning; 早上好;
I´m trying to load some values from a mysql database to a "select", inside a form with HTML, PHP and some javascript. 我正在尝试将一些值从mysql数据库加载到带有HTML,PHP和一些javascript的表单内的“选择”中。
I´m doing a school project and I need some names that are registered on the database. 我正在做一个学校项目,我需要一些在数据库中注册的名称。 When I click on the select i want them to appear, so I can register one class and give them a class director (i´m not sure if that is called like that in english).
当我单击选择项时,我希望他们出现,因此我可以注册一个班级并给他们一个班级主任(我不确定这是否用英语来称呼)。 When I "edit" the class, i want the select to put the value that are on the database and I want it to let me change the value.
当我“编辑”该类时,我希望选择将值放在数据库中,并且希望它让我更改值。
I have a function file that help me with the functions that I use on the website, and I made de select code like this. 我有一个功能文件,可以帮助我使用网站上的功能,并且我制作了de select这样的代码。
function DBConnect ()
{
$db = mysqli_connect (DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die (mysqli_connect_error());
return $db;
}
function DBClose($db)
{
mysqli_close($db) or die (mysqli_error($db));
}
function DBExecute($sql)
{
$db = DBConnect();
$result = mysqli_query($db,$sql);
DBClose($db);
return $result;
}
function DBRead11()
{
$sql="SELECT nome FROM user_especial";
$result=DBExecute($sql);
while($res=mysqli_fetch_assoc($result))
{
$dts[]=$res;
}
return $dts;
}
And the code that I used on the php page so the select could "spawn" the values is this one: 我在php页面上使用的代码就是这样的代码:
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php echo $dts; ?>
</select>
My database looks like the table that I puted on the first imgur link. 我的数据库看起来像我放在第一个imgur链接上的表。 Can someone help me?
有人能帮我吗? Thank you.
谢谢。
If I get your problem correctly the issue is that $dts
is not known in the HTML select element because it is only in the function scope of DBRead11
. 如果我正确地解决了您的问题,那么问题在于HTML的select元素中不知道
$dts
,因为它仅在DBRead11
的功能范围内。 Therefore, before getting the value of $dts
, you need to call the function DBRead11()
and save the returned value into $dts
. 因此,在获取
$dts
的值之前,需要调用函数DBRead11()
并将返回的值保存到$dts
。 After that, because the array $dts
should contain the attribute nome
of all rows in the database, you can loop through all entries of $dts
(eg with foreach()
) and construct a HTML option element from the value at the specific index. 之后,由于数组
$dts
应该包含数据库中所有行的属性nome
,因此可以遍历$dts
所有条目(例如,使用foreach()
),并根据特定索引处的值构造HTML选项元素。
In addition to that, I think you need to initialize $dts
in your DBRead11
function as $dts = array()
or $dts = []
before adding values to it. 除此之外,我认为你需要初始化
$dts
在DBRead11
功能为$dts = array()
或$dts = []
增加值之前。
function DBRead11() {
$sql="SELECT nome FROM user_especial";
$result=DBExecute($sql);
$dts = mysqli_fetch_assoc($result);
return $dts;
}
<?php $dts = DBRead11(); ?>
<select name="diretor_turma" id="diretor_turma" required class="input-field4">
<?php
foreach($dts as $option) {
?>
<option value="<?php echo $option['nome']; ?>"><?php echo $option['nome']; ?></option>
<?php
}
?>
</select>
As @M.Hemant already asked, please provide a var_dump()
of the $dts
variable. 如@ M.Hemant所问,请提供
$dts
变量的var_dump()
。 Otherwise, I can't tell exactly if just outputting $option
(every a single element of $dts) is sufficient. 否则,我无法确切判断仅输出
$option
($ dts的每个元素)是否足够。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.