duanniubeng2265
2016-11-20 13:12
浏览 406
已采纳

MySQL数据库中预先选择的下拉列表值

Though I have researched, I couldn't find any solution for this. I need to get the database value ("Default") as the pre-selected value of the drop down list.

<select name="listCustomer" id="listCustomer">
    <?php 
    $sql = mysqli_query($connection,"SELECT customer_name FROM customers");
    while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
    echo "<option value=\"" .   $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";}
    ?>
</select>

Can you please help me on this?

图片转代码服务由CSDN问答提供 功能建议

虽然我已经研究过,但我找不到任何解决办法。 我需要将数据库值(“Default”)作为下拉列表的预选值。

 &lt; select name =“listCustomer”id =“listCustomer  “&gt; 
&lt;?php 
 $ sql = mysqli_query($ connection,”SELECT customer_name FROM customers“); 
 while($ row = mysqli_fetch_array($ sql,MYSQLI_ASSOC)){
 echo”&lt; option  value = \“”。$ row ['customer_name']。“\”&gt;“  。  $ row ['customer_name']。  “&lt; / option&gt;”;} 
?&gt; 
&lt; / select&gt; 
   
 
 

您可以帮我解决这个问题吗? \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudun1934 2016-11-20 13:15
    已采纳

    Just create a variable before the echo, something like:

    $selected = ((strtolower($row['customer_name']) == 'default') ? 'selected' : '');
    

    then change the echo to this:

    echo '<option '.$selected.' value="'.$row['customer_name'].'">'.$row['customer_name'].'</option>';
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshui2254 2016-11-20 13:15

    This can be accomplished using an if statement on the customer_name

    $sql = mysqli_query($connection,"SELECT customer_name FROM customers");
    while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
         if($row["customer_name"] === "Default"){
          echo "<option value=\"" .   $row['customer_name'] . "\" selected>" . $row['customer_name'] . "</option>";
         } else {
           echo "<option value=\"" .   $row['customer_name'] . "\">" . $row['customer_name'] . "</option>";
         }
     }
     ?>
    

    Note the selected tag on the first echo.

    评论
    解决 无用
    打赏 举报
  • dsfd3546 2016-11-20 13:55
    $query="SELECT customer_name FROM customers";
    $result = @mysql_query ($query);
    echo "<select name=customer_name value=' '>";
    while($drop=@mysql_fetch_array($result)){
    echo "<option value=$drop[customer_name]>$drop[customer_name]</option>";
    }
    echo "</select>";
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题