[英]php dropdown list from mysql
I just started learning php last week and I am trying to populate values of a drop down list in my html form from mysql database. 我上周刚开始学习php,我正尝试从mysql数据库的html表单中填充下拉列表的值。 the dropdown shows but it only shows "choose" as an option.
下拉列表显示,但仅显示“选择”作为选项。 what am I doing wrong, any help would be march appreciated.
我做错了什么,任何帮助将不胜感激。 Below is the problem part of my code.
以下是我的代码的问题部分。
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT event_id, event_name FROM events";
$results= mysqli_query( $dbc, $query);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["event_id"];
$event=$row["event_name"];
$options ="<OPTION VALUE=\"$id\">".$event;
}
?>
<SELECT NAME=eventid>
<OPTION VALUE=0>Choose
<?php echo $options ?>
</SELECT>
</form>
change 更改
$options ="<OPTION VALUE=\"$id\">".$event;
to 至
$options .="<OPTION VALUE=\"$id\">".$event;
to concatenate instead of changing the value always.. also consider closing the option tag.. 串联而不是总是更改值。也可以考虑关闭选项标签。
Include correction after Benedikt Olek's answer. 在Benedikt Olek的回答之后包括更正。
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
require_once('connectvars.php');
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$query = "SELECT event_id, event_name FROM events";
$db = mysql_select_db(DB_NAME);
$results= mysql_query( $dbc, $query);
?>
<SELECT NAME=eventid>
<OPTION VALUE=0>Choose</OPTION>
<?php
while($row = mysql_fetch_array($results)
echo "<option value=\"" . $row['event_id'] . "\">" . $row['event_name'] . "</option>";
?>
</SELECT>
</form>
This is the mysql-driver solution. 这是mysql驱动程序解决方案。
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
require_once('connectvars.php');
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db = mysql_select_db(DB_NAME);
$results= mysql_query("SELECT event_id, event_name FROM events");
?>
<select name="eventid">
<option value="0">Choose</OPTION>
<?php
while($row = mysql_fetch_array($results)) {
echo '<option value="'.$row['event_id'].'">'. $row['event_name'].'</option>';
}
?>
</select>
</form>
try this one : 试试这个:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<SELECT NAME=eventid>
<?php
require_once('connectvars.php');
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
$query = "SELECT event_id, event_name FROM events";
$results= mysql_query( $dbc, $query);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["event_id"];
$event=$row["event_name"];
echo "<OPTION VALUE=".$id\.">".$event."</option>";
}
?>
</SELECT>
</form>
Since "Choose" is a hardcoded option it is always displayed. 由于“选择”是一个硬编码选项,因此始终显示。 You are not getting more results because the Variable
$options
is empty. 您不会获得更多结果,因为Variable
$options
为空。 You can verify that by using var_dump($options)
. 您可以使用
var_dump($options)
。
The reason for your empty variable is the while-iteration. 空变量的原因是while迭代。 Your function
mysql_fetch_array()
never returns true. 您的函数
mysql_fetch_array()
永远不会返回true。
That is because you are mixing up drivers. 那是因为您混淆了驱动程序。 You are connecting via
mysqli
, hence mysql_fetch_array is not working. 您正在通过
mysqli
连接,因此mysql_fetch_array无法正常工作。
You have two options: change to use the mysql-driver for connection or recode the iteration to make use of the mysqli-api. 您有两个选择:更改为使用mysql驱动程序进行连接,或者重新编码迭代以使用mysqli-api。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.