如何使用php中的下拉菜单从数据库中获取数据并将其显示在标签上?

时间:2018-10-26 03:53:01

标签: php mysql

我想使用从数据库生成的下拉列表,将数据库中的检索数据显示到我的输入区域中。问题是当运行我的代码时,customername输入区域消失了。你能帮我这些吗?

Dropdown 注意:我的Dropdown正常运行,我只添加了它。

<?php
           $connect = mysqli_connect("localhost", "root", "", "xls_db");  
         ?>
             <select name="customercode" id="customercode" class="form-control"> 
             <option value="" > -----------Customer Code----------- </option> 
         <?php
            $dd_res=mysql_query("Select DISTINCT customercode from cr18_cust_listing WHERE Status = 'Active' ");
            while($r=mysql_fetch_row($dd_res))
            { 
               echo "<option value='$r[0]'> $r[0] </option>";
            }
         ?>
            </select>

PHP以显示文本

<?php
        $connect = mysqli_connect("localhost", "root", "", "xls_db");  
         if(isset($_POST["customercode"]))  
        {  
      if($_POST["customercode"] != '')  
      {  
           $sql = "SELECT * FROM cr18_cust_listing WHERE customercode = '".$_POST["customercode"]."'";  
      }  
      $result = mysqli_query($connect, $sql);  

     while($r = mysqli_fetch_array($result))  
      {  


      ?>
        <div class="form group has-feedback">
            <input class="form-control" type="text" name="accntname" required="required" placeholder="Customer Name" value="<?php echo $r['customername'] ?>"><span class="glyphicon glyphicon-user form-control-feedback"></span><br />
            </div>
<?php }
       }    ?>

缺少客户名称输入区域

Screenshot

2 个答案:

答案 0 :(得分:0)

尝试以下代码您已关闭文本区域而未打开它。

<input class="form-control" name="accntname" required="required" placeholder="Customer Name" value="<?php echo $r['customername'] ?>"><span class="glyphicon glyphicon-user form-control-feedback"></span><br />

答案 1 :(得分:0)

下拉菜单

<?php
$conn=new mysqli("localhost","root","","xls_db");
if($conn->connect_error)
{
     echo $conn->connect_error;
     die("sorry database connection failed");
}
         ?>
                <select name="customercode" id="customercode" class="form-control" required>
                <option value="" > -----------Customer Code----------- </option>
    <?php

$sql = "Select DISTINCT customercode from cr18_cust_listing WHERE Status = 'Active'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<option value='";
        echo $row['customercode'];
        echo "'>";
        echo $row['customercode'];
        echo "</option>";
    }
}
    ?>

    </select>

就可以了,这可以肯定