【问题标题】:Set selected value as selected when choosing from msql dropdown从 msql 下拉列表中选择时将所选值设置为已选择
【发布时间】:2020-04-11 19:12:46
【问题描述】:

我开始了 php 课程。现在我已经“学习”了如何将 sql-data 显示到 html-table 中。以下问题现在超出了我的知识范围......但我仍然有兴趣解决它,因为它会对我有所帮助。我找到了一些解决方案,但与mysql无关。

我有一个包含三列的 mysql-view:Brewery、City 和 Beer。 我想让客户可以从下拉列表中选择城市。为此,我创建了一个表单操作:

<?php $CityShow = 'All' ?>;

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
    <h2>City</h2>
    <?php
    $resultSet = $Verbindung->query("SELECT DISTINCT City FROM breweries");
    ?>
    <select name = 'City'>
    <?php
       echo"<option selected value = $City>All</option>";
       while ($rows = $resultSet->fetch_assoc())
       {
          $City = $rows ['City'];
          echo"<option value = '{$City}'>{$City}</option>";
       };
    ?>
    <input type='submit' value = 'Send'>
    <!-- <?php $CityShow = $_POST["City"]; ?>; -->
  </select>

这很好用,客户可以选择城市,表格是动态构建的。

<?php
if($CityShow == '' || $CityShow == 'All'){
$SQL = "SELECT * from Breweries";
}
else{
  $SQL = "SELECT * from Breweries where city = '$CityShow'";   
}

$Ergebnis =  $Verbindung->query($SQL);
?>
<!-- fill table-->
<?php
if($Ergebnis->num_rows > 0) {
echo "<table><tr><th>Brewery</th><th>City</th><th>Beer</th></tr>";
   while($row = $Ergebnis->fetch_assoc()){
   echo"<tr>";
   echo "<td>" . $row['Brewery'] . "</td>";
   echo "<td>" . $row['City'] . "</td>";
   echo "<td>" . $row['Beer'] . "</td>";
   echo"</tr>";
  }
  echo "</table>";
 }
 ?>

我想,这不是最好的方法,但它对我有用。

我的问题: 目前,当使用提交按钮选择一个城市时,焦点总是变回“全部”(这是一个选项,不在数据库中 - 其目的是显示所有记录)。我想保留专注于所选城市。

伊夫

【问题讨论】:

  • 警告:使用mysqli 时,您应该使用parameterized queries 和bind_param 将任何数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了严重的SQL injection bug。 切勿将$_POST、$_GET或任何类型的数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。

标签: php mysql select


【解决方案1】:

在您通常输入静态属性selected 的选项标记中,您输入一个条件来检查该值是否已设置,如果已设置,则回显属性selected。

下面是一个静态选择/选项将设置值设置为选中的示例。

<select class="form-control" name="city">
    <option value="San Diego"<?php if($row['City'] === "San Diego"){ echo ' selected'; } ?>>San Diego</option>
    <option value="Austin"<?php if($row['City'] === "Austin"){ echo ' selected'; } ?>> Austin</option>
    <option value="New Orleans"<?php if($row['City'] === "New Orleans"){ echo ' selected'; } ?>>New Orleans</option>
    <option value="Portland"<?php if($row['City'] === "Portland"){ echo ' selected'; } ?>>Portland</option>
    <option value="Detroit"<?php if($row['City'] === "Detroit"){ echo ' selected'; } ?>>Detroit</option>                                    
</select>

如果您的选择是使用城市数组动态创建的,并且您希望遍历该数组并找到在 $row 数组中设置的集合城市,您可以遍历数组并设置查找集合的条件来自数据库的值:

//start your select and assign to a variable
$stmt = '<select class="form-control" name="city">';
// Run the array of cities through a loop
foreach($breweries as $city){
    //if the value from DB $row['City'] is equal to a value in the array set selected in the  
    //option tag else we display without selected attribute in the option tag
    if($row['City'] === $city){
        $stmt .= '<option value="'.$city.'" selected>'.$city.'</option>';
    }else{
        $stmt .= '<option value="'.$city.'">'.$city.'</option>';
    }
}
//close the select tag
$stmt .= '</select>';

【讨论】:

  • 这需要一个数组和一个循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多