php 如何用 mysql 数据填充 html 组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2770219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:38:30  来源:igfitidea点击:

How to populate html combo box with mysql data

phpmysqlhtml

提问by user225269

Please help, I'm having trouble loading mysql data on the combo box. The data that I'm loading is 1 column from a table. Here is my current code, and it crashed firefox for some reason:

请帮忙,我在组合框上加载 mysql 数据时遇到问题。我正在加载的数据是表中的 1 列。这是我当前的代码,它由于某种原因导致 Firefox 崩溃:

<td colspan=”2″>Religion</TD>
<td>
<select name="REL" onClick="submitCboSemester();">
<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
while($query_data = mysql_fetch_array($result_disp))
{
?>
<option value="<? echo $query_data["RID"]; ?>"<?php if ($query_data["RID"]==$_POST['REL']) {?>selected<? } ?>><? echo $query_data["RELIGION"]; ?></option>
<? } ?>
</select>
</td>

The column is RELIGIONand it ID is RIDHow do I populate the combo box with all the data in the column RELIGION

列是RELIGION,它的 ID 是RID如何使用列宗教中的所有数据填充组合框

回答by Jacob Relkin

Chances are, you don't have short_open_tagenabled in php.ini, this is probably what was causing your code to fail.

很有可能,您没有short_open_tag启用php.ini,这可能是导致您的代码失败的原因。

Also, you should output selected="selected", not just selected

此外,您应该输出selected="selected",而不仅仅是selected

Try this:

尝试这个:

<td colspan=”2″>Religion</TD>
<td>
<select name="REL" onClick="submitCboSemester();">
<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
while($query_data = mysql_fetch_array($result_disp))
{
?>
<option value="<?php echo $query_data["RID"]; ?>"<?php if ($query_data["RID"]==$_POST['REL']) {?> selected="selected"<? } ?>><?php echo $query_data["RELIGION"]; ?></option>
<? } ?>
</select>
</td>

回答by Usha

<html>
<title>
demo </title>
<body>
<?
// Load field datas into List box
$cn=mysql_connect("localhost",root) or die("Note: " . mysql_error());
echo "Conn ok<br>";
$res=mysql_select_db("test",$cn) or die("Note: " . mysql_error());
echo " Database opened<br>";
$q="insert into usha2 (`sno`, `city name`) values (NULL, 'Nagercoil');";
//$q="INSERT into `test`.`usha2` (`sno`, `city name`) VALUES (NULL, 'Delhi');";
$res=mysql_query($q) or die("Note: " . mysql_error());
echo " record inserted<br>";
$res=mysql_query("select * from usha2;") or die("Note: " . mysql_error());
echo " qry executed<br>";
?>
<select name="pno" size=1>
<?
while($ri = mysql_fetch_array($res))
{
echo "<option value=" .$ri['city name'] . ">" . $ri['city name'] . "</option>";
}
echo "</select> "
?>
</body>

回答by mexique1

Extract the data before displaying it !

在显示之前提取数据!

And also, use the PHP Alternative Syntaxwhen mixing PHP & HTML :

而且,在混合 PHP 和 HTML 时使用PHP 替代语法

<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
$options = array();
while ($query_data = mysql_fetch_array($result_disp)) {
    $options[$query_data["RID"]] = $query_data["RELIGION"];
}
?>

<select name="REL" onClick="submitCboSemester();">
<?php foreach ($options as $key => $value) : ?>
    <?php $selected = ($key == $_POST['REL']) ? 'selected="selected"' : ''; ?>
    <option value="<?php echo $key ?>" <?php echo $selected ?>>
    <?php echo $value ?>
    </option>
<?php endforeach; ?>
</select>

回答by Joshua Pinter

Looks like there is no space between the end of your value and the selected.

看起来您的值的末尾和selected.

回答by castor520

When you pull the data from your database you should use the following format as the format you are currently using is being phase out as MySqli advances

当您从数据库中提取数据时,您应该使用以下格式,因为随着 MySqli 的进步,您当前使用的格式正在逐步淘汰

    $religion = $db->query("SELECT Religion FROM TableName ORDER BY RID");

$db is a connection to the database created as such

$db 是到这样创建的数据库的连接

    $db = new Mysqli($server, $username, $password, $database);

Just set the variable to the correct value.

只需将变量设置为正确的值即可。