Php 使用MySQL数据库中的值填充HTML下拉列表,并在列表中有其他选项时将其“选中”

Php 使用MySQL数据库中的值填充HTML下拉列表,并在列表中有其他选项时将其“选中”,php,html,mysql,forms,drop-down-menu,Php,Html,Mysql,Forms,Drop Down Menu,假设我有一个HTML下拉菜单 <p>General Medical History: <select name="otherproblem"> <option value="Nothing">Nothing</option> <option value="Allergy: Penicillin">Allergy: Penicillin</option> <option value="Aspir

假设我有一个HTML下拉菜单

<p>General Medical History: 
<select name="otherproblem">
    <option value="Nothing">Nothing</option>
    <option value="Allergy: Penicillin">Allergy: Penicillin</option>
    <option value="Aspirin">Aspirin</option>
    <option value="Erythromycin">Erythromycin</option>
    <option value="Latex or Rubber Products">Latex or Rubber Products</option>
    <option value="Codeine">Codeine</option>
    <option value="Tetracycline">Tetracycline</option>
    <option value="Germicides/Pesticides, Foods">Germicides/Pesticides, Foods</option>
    <option value="Other">Other</option>
    <option value="Asthma">Asthma</option>
    <option value="Bleeding Disorders">Bleeding Disorders</option>
    <option value="Diabetes">Diabetes</option>
    <option value="Epilepsy">Epilepsy</option>
    <option value="GI disorders">GI disorders</option>
    <option value="Heart disease">Heart disease</option>
    <option value="Hepatitis">Hepatitis</option>
    <option value="Jaundice">Jaundice</option>
    <option value="Liver disease">Liver disease</option>
    <option value="Neoplasm">Neoplasm</option>
    <option value="Psychiatric Problems">Psychiatric Problems</option>
    <option value="Respiratory diseases">Respiratory diseases</option>
    <option value="Rheumatic fever">Rheumatic fever</option>
</select>
</p>
通过选择其中一个选项,我将特定值作为VARCHAR添加到数据库中


现在我把它们叫回来,比方说,进行编辑,所以我需要将输入的数据返回到同一个表单,在那里我可以将所有其他内容检索到文本框中。但是我不知道如何从db中获取值,并在这个下拉列表中选择它。我所能做的就是将从db检索到的值保存到名为$otherproblem的变量中。您可以修改每个选项标记,使其如下所示:

<option value="Nothing" <?=$otherproblem === 'Nothing' ? 'selected="selected"' : ''?>>Nothing</option>
<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<option selected="selected" value="<?=$otherproblem;?>"><?=$otherproblem;?></option>
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
?>
<option value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>

每个选项都应该将数据库中的值与选项标记的值进行比较,如果它们相等,则添加所选属性。

您可以修改每个选项标记,使其如下所示:

<option value="Nothing" <?=$otherproblem === 'Nothing' ? 'selected="selected"' : ''?>>Nothing</option>
<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<option selected="selected" value="<?=$otherproblem;?>"><?=$otherproblem;?></option>
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
?>
<option value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>

每个人都应该将数据库中的值与选项标记的值进行比较,如果它们相等,则添加所选属性。

通常,您也会从数据库中加载选项列表。您将在列表中循环并动态构造标记。在执行此操作时,将创建的选项值与从先前提交中检索到的值进行比较,如果它们匹配,则将所选属性添加到选项标记。

通常,您也会从数据库加载选项列表。您将在列表中循环并动态构造标记。执行此操作时,将创建的选项值与先前提交的值进行比较,如果匹配,则将所选属性添加到选项标记中。

以下是一种方法:

<?php

$otherproblem = "Aspirin";

$dropdown = array(
    "Nothing",
    "Allergy: Penicillin",
    "Aspirin",
    "Erythromycin",
    "Latex or Rubber Products",
    "Codeine",
    "Tetracycline",
    "Germicides/Pesticides, Foods",
    "Other",
    "Asthma",
    "Bleeding Disorders",
    "Diabetes",
    "Epilepsy",
    "GI disorders",
    "Heart disease",
    "Hepatitis",
    "Jaundice",
    "Liver disease",
    "Neoplasm",
    "Psychiatric Problems",
    "Respiratory diseases",
    "Rheumatic fever"
);

?>
<p>General Medical History: 
<select name="otherproblem">
    <?php foreach ($dropdown as $name): ?>
        <option <?php if ($otherproblem == $name) print('selected="selected"');?> value="<?php print($name);?>"><?php print($name);?></option>
    <?php endforeach; ?>
</select>
</p>
方式2:


以下是一种方法:

<?php

$otherproblem = "Aspirin";

$dropdown = array(
    "Nothing",
    "Allergy: Penicillin",
    "Aspirin",
    "Erythromycin",
    "Latex or Rubber Products",
    "Codeine",
    "Tetracycline",
    "Germicides/Pesticides, Foods",
    "Other",
    "Asthma",
    "Bleeding Disorders",
    "Diabetes",
    "Epilepsy",
    "GI disorders",
    "Heart disease",
    "Hepatitis",
    "Jaundice",
    "Liver disease",
    "Neoplasm",
    "Psychiatric Problems",
    "Respiratory diseases",
    "Rheumatic fever"
);

?>
<p>General Medical History: 
<select name="otherproblem">
    <?php foreach ($dropdown as $name): ?>
        <option <?php if ($otherproblem == $name) print('selected="selected"');?> value="<?php print($name);?>"><?php print($name);?></option>
    <?php endforeach; ?>
</select>
</p>
方式2:


您有两个选项,一个是沿以下行加载列表:

<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
if(strcmp($row['Problem'],$otherproblem)===0){ $selected = 'selected="selected"'; } else { $selected = ""; }
?>
<option <?=$other?> value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>
脏,但它将与您当前的设置方式配合使用。您也可以通过简单地在列表顶部选择项目,然后按如下方式选择完整列表来装配它:

<option value="Nothing" <?=$otherproblem === 'Nothing' ? 'selected="selected"' : ''?>>Nothing</option>
<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<option selected="selected" value="<?=$otherproblem;?>"><?=$otherproblem;?></option>
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
?>
<option value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>

有很多更简洁的方法可以做到这一点,但在不了解如何提取数据的情况下,这两个选项在任何情况下都会起作用。

您有两个选项,一个是沿着以下行加载列表:

<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
if(strcmp($row['Problem'],$otherproblem)===0){ $selected = 'selected="selected"'; } else { $selected = ""; }
?>
<option <?=$other?> value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>
脏,但它将与您当前的设置方式配合使用。您也可以通过简单地在列表顶部选择项目,然后按如下方式选择完整列表来装配它:

<option value="Nothing" <?=$otherproblem === 'Nothing' ? 'selected="selected"' : ''?>>Nothing</option>
<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<option selected="selected" value="<?=$otherproblem;?>"><?=$otherproblem;?></option>
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
?>
<option value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>

有更简洁的方法可以做到这一点,但在不了解如何提取数据的情况下,这两个选项在任何情况下都会起作用。

我在文件中运行一个查询,$recall=mysql\u querySELECT*FROM history,其中id='$id'或diemsql\u error$getdetails=mysql\u fetch\u array$recall$otherproblem=$getdetails['otherproblem'];当我尝试@tmpmember的方法时,我遇到了一个解析错误。这对我来说似乎是一个简单的方法;虽然$row=mysql_fetch_array$result{$otherproblem=$row['otherproblem'];}它应该可以正常工作。我现在正在运行这段代码作为一个测试,只要查询包含一个结果,这段代码似乎就可以了。理论上,这段代码应该可以工作。你的错误是什么?对不起,我正在文件中运行一个查询,$recall=mysql\u querySELECT*FROM history,其中id='$id'或diemsql\u错误$getdetails=mysql\u fetch\u array$recall$otherproblem=$getdetails['otherproblem'];当我尝试@tmpmember的方法时,我遇到了一个解析错误。这对我来说似乎是一个简单的方法;虽然$row=mysql_fetch_array$result{$otherproblem=$row['otherproblem'];}它应该可以正常工作。我现在正在运行这段代码作为一个测试,只要查询包含一个结果,这段代码似乎就可以了。理论上,这段代码应该可以工作。你的错误是什么?试着向你道歉,谢谢你的帮助@J.Money。我会调查的。对不起,我不能投票支持你的答案。我还没有得到足够的声望点数。谢谢你和@J.Money的帮助。我会调查的。对不起,我不能投票支持你的答案。我还没有足够的声望点数。谢谢你。但是我得到了一个解析错误。不知道:谢谢你。但是我得到了一个解析错误。我不知道:如果你没有启用短标签,那么你需要更改。是的,我有一个不同的错误。我回音htlm表格时出错了。现在明白了。非常感谢:如果你没有启用短标签,那么你需要更改。是的,我有一个不同的错误。我回音htlm表格时出错了。现在明白了。谢谢: