PHP将数据回显到HTML下拉菜单中

PHP将数据回显到HTML下拉菜单中,php,html,mysql,Php,Html,Mysql,我有一个HTML等等。。标签现在我想实现的是在选择ie之后。我想将相关信息从数据库加载到一个新的标签中,并使用尽可能多的标签 我现在正在使用PHP来实现这一点,例如,如果我选择option1,那么它后面的查询将检索相关信息并将其存储在数组中,如果我选择option2,则完全相同 我做的下一步是创建一个循环来显示array()的结果,但我正在努力找到正确的解决方案,将检索到的数据回送到etc中,因为这不是我的强项 希望你明白我在努力实现什么,下面的代码会把事情弄清楚 HTML: 请选择一个车间

我有一个HTML等等。。标签现在我想实现的是在选择ie之后。我想将相关信息从数据库加载到一个新的标签中,并使用尽可能多的标签

我现在正在使用PHP来实现这一点,例如,如果我选择option1,那么它后面的查询将检索相关信息并将其存储在数组中,如果我选择option2,则完全相同

我做的下一步是创建一个循环来显示array()的结果,但我正在努力找到正确的解决方案,将检索到的数据回送到etc中,因为这不是我的强项

希望你明白我在努力实现什么,下面的代码会把事情弄清楚

HTML:


请选择一个车间
外汇
二进制选项
PHP:

$form=Array();
如果(isset($_POST['workshop'])){
$form['workshop']=$_POST['workshop'];
$form['forex']=$u POST['forex'];
$form['binary']=$_POST['binary'];
//检索二进制工作坊
如果($form['workshop']=='Forex'){
$sql2=“从课程中选择id、课程、位置,其中课程类似于“%Forex%”或课程类似于“&Forex%”;
$query2=mysqli\u查询($link,$sql2);
而($result2=mysqli\u fetch\u assoc($query2)){
//我遇到的问题是:/
回声“;
回显“$result[1]”;
回声“;
打印($result2);回显“
”; } }否则{ $sql=“从课程(如“%Binary%”或课程(如“%Binary%”)中选择id、课程、位置”; $query=mysqli\u查询($link,$sql); while($result=mysqli\u fetch\u assoc($query)){ 打印($result);回显“
”; } } }
尝试以下代码:

$query2 = mysqli_query($link, $sql2);

echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
  echo "<option value='oko'>{$result['course']}</option>";

}
echo "</select>";
echo '</br>';
$query2=mysqli\u查询($link,$sql2);
回声“;
而($result2=mysqli\u fetch\u assoc($query2)){
回显“{$result['course']}”;
}
回声“;
回音“
”;
从php的顶部开始:

// not seeing uses of the $form I removed it from my answer

if(isset($_POST['workshop'])){

$workshop = $_POST['workshop'];
$lowerWorkshop = strtolower($workshop);
// neither of $_POST['Forex'] nor $_POST['Binary'] are defined in your POST. you could as well remove those lines?    

//Retrieve Binary Workshops HERE we can define the sql in a single line:
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%$workshop%' OR  course LIKE '&$lowerWorkhop%'";       
$query = mysqli_query($link, $sql); // here no need to have two results

// Here lets build our select first, we'll echo it later.
$select = '<select id="$workshop" name="$workshop" style="display: none">';
while($result = mysqli_fetch_assoc($query)){
    $select.= '<option value="' . $result['id'] . '">' . $result['course'] . '</option>';
// here I replaced the outer containing quotes around the html by single quotes.
// since you use _fetch_assoc the resulting array will have stroing keys. 
// to retrieve them, you have to use quotes around the key, hence the change
    }
$select.= '</select>';
}
echo $vSelect;
//没有看到$form的用法,我从答案中删除了它
如果(isset($_POST['workshop'])){
$workshop=$_POST['workshop'];
$lowerWorkshop=strtolower($workshop);
//您的帖子中既没有定义$\u POST['Forex']也没有定义$\u POST['Binary']。您还可以删除这些行吗?
//在这里,我们可以在一行中定义sql:
$sql=“从课程“%$workshop%”或课程“&$lowerWorkhop%”中选择id、课程、位置;
$query=mysqli_query($link,$sql);//这里不需要有两个结果
//在这里,让我们先构建我们的select,稍后我们将响应它。
$select='';
while($result=mysqli\u fetch\u assoc($query)){
$select.=''.$result['course'].';
//在这里,我用单引号替换了html中包含的外部引号。
//由于使用了_fetch_assoc,因此生成的数组将具有strong键。
//要检索它们,必须在键周围使用引号,因此更改
}
$select.='';
}
echo$vSelect;

这将输出一个select,其中包含由任一查询返回的每一行的一个选项。顺便说一下,这个特定的示例不会在屏幕上显示任何内容(因为您选择的显示设置为“无”)。但是请查看源代码以检索示例。

{$row_list['ourse']}课程缺少一个c?很抱歉,这一行没有包含在内,我现在的问题基本上是将所有结果作为下拉菜单进行回显,您的
while
循环正在生成多个具有相同ID“Forex”的选择菜单,并且所有值都具有“oko”值。这不好,因为ID是唯一的。要回显多个菜单还是仅回显一个菜单?我要回显一个包含多个选项的下拉菜单。
$query2 = mysqli_query($link, $sql2);

echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
  echo "<option value='oko'>{$result['course']}</option>";

}
echo "</select>";
echo '</br>';
// not seeing uses of the $form I removed it from my answer

if(isset($_POST['workshop'])){

$workshop = $_POST['workshop'];
$lowerWorkshop = strtolower($workshop);
// neither of $_POST['Forex'] nor $_POST['Binary'] are defined in your POST. you could as well remove those lines?    

//Retrieve Binary Workshops HERE we can define the sql in a single line:
$sql = "SELECT id, course, location FROM courses WHERE course LIKE '%$workshop%' OR  course LIKE '&$lowerWorkhop%'";       
$query = mysqli_query($link, $sql); // here no need to have two results

// Here lets build our select first, we'll echo it later.
$select = '<select id="$workshop" name="$workshop" style="display: none">';
while($result = mysqli_fetch_assoc($query)){
    $select.= '<option value="' . $result['id'] . '">' . $result['course'] . '</option>';
// here I replaced the outer containing quotes around the html by single quotes.
// since you use _fetch_assoc the resulting array will have stroing keys. 
// to retrieve them, you have to use quotes around the key, hence the change
    }
$select.= '</select>';
}
echo $vSelect;