PHP 下拉搜索结果选择错误的输入

我制作了一个下拉搜索表单,该表单由我的数据库内容自动填充。表格中的声音将是例如具有不同尺寸的木材类型。因此,存在具有不同数据的可重复木材名称。为避免重复,下拉列表中填充有待选择的木材类型,然后与其所有变体一起显示。问题是,在选择输入时,结果是上面列出的项目,而不是选定的项目。


<form action="search2.php" method="POST">

    <select name="finit"  onchange='this.form.submit()'>

      <?php

      include("connect.php");

      $query = "SELECT finit FROM prime";

      $info = mysqli_query($conn, $query);

      $finit = '';


         echo "<option value=\"\">Selezione Materiale</option>";

        while($row = $info->fetch_assoc()){

if($row['finit'] != $finit) {

    echo "<option value=\"$finit\">" . $row['finit'] . "</option>";

    $finit = $row['finit']; 

                            } 

    } 

       ?>

    </select>

    <noscript><input type="submit" value="Submit"></noscript>

  </form>


蝴蝶不菲
浏览 153回答 2
2回答

慕侠2389804

由于有许多与单一木材类型相关的变体(尺寸),您必须首先将木材类型作为用户输入(通过下拉列表),然后您可能希望显示该特定木材的所有可能变体(尺寸)木材类型。因此,按以下方式更改 SQL 查询,$query = "SELECT DISTINCT finit FROM prime";以及while以下方式的循环,while($row = $info->fetch_assoc()){&nbsp; &nbsp; $output = "<option value='" . $row['finit'] . "'";&nbsp; &nbsp; if($row['finit'] == $_POST['finit']){&nbsp; &nbsp; &nbsp; &nbsp; $output .= " selected='selected'";&nbsp; &nbsp; }&nbsp; &nbsp; $output .= ">" . $row['finit'] . "</option>";&nbsp; &nbsp; echo $output;}&nbsp;

撒科打诨

试试这个,但如果条件根据您的默认值和 sql 值应该匹配,则进行更改。&nbsp; &nbsp; <form action="search2.php" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; <select name="finit"&nbsp; onchange='this.form.submit()'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; include("connect.php");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = "SELECT finit FROM prime";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $info = mysqli_query($conn, $query);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $finit = '';&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="">Selezione Materiale</option>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($row = $info->fetch_assoc()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($row['finit'] == $finit) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $selected = 'selected';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $selected = '';&nbsp;$finit = $row['finit'];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<option value="<?php echo $finit ?>" <?php echo $selected ?>><?php echo $row['finit']?></option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php } ?>&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; <noscript><input type="submit" value="Submit"></noscript>&nbsp; &nbsp; &nbsp; </form>
打开App,查看更多内容
随时随地看视频慕课网APP