【问题标题】:Fetching data from MySQL database to html dropdown list从 MySQL 数据库获取数据到 html 下拉列表
【发布时间】:2012-04-18 01:31:14
【问题描述】:

我有一个包含 html 表单的网站,在这个表单中,我有一个下拉列表,其中包含在公司工作的代理列表,我想从 MySQL 数据库中获取数据到这个下拉列表,所以当你添加一个新代理时,他的名称将作为选项出现在下拉列表中。

你能帮我编写这个php代码吗,谢谢

<select name="agent" id="agent">
</select>  

【问题讨论】:

  • 你能把你的 MySQL 表的结构贴出来吗?
  • 数据库名称是“代理”,包含代理名称的表是“代理”这是表代理 id_agent int(20) login_agent varchar(20) latin1_swedish_ci mdp_agent varchar(20) 的字段latin1_swedish_ci nom_agent varchar(20) latin1_swedish_ci prenom_agent varchar(20) latin1_swedish_ci tel_agent int(11) 非 mail_agent varchar(20) latin1_swedish_ci adresse_agent 文本 latin1_swedish_ci

标签: php mysql html


【解决方案1】:

你的问题很简单

  1. 对您的数据库执行查询以获取结果集或使用 API 获取结果集

  2. 循环遍历结果集或仅使用 php 遍历结果

  3. 在每次迭代中,只需将输出格式化为一个元素

以下参考应该会有所帮助

HTML option tag

Getting Datafrom MySQL database

希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    为此,您需要遍历查询结果的每一行并将此信息用于每个下拉选项。您应该能够相当容易地调整下面的代码以满足您的需求。

    // Assume $db is a PDO object
    $query = $db->query("YOUR QUERY HERE"); // Run your query
    
    echo '<select name="DROP DOWN NAME">'; // Open your drop down box
    
    // Loop through the query results, outputing the options one by one
    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
       echo '<option value="'.$row['something'].'">'.$row['something'].'</option>';
    }
    
    echo '</select>';// Close your drop down box
    

    【讨论】:

    • value和文字相同时,value属性不需要声明。
    【解决方案3】:
    # here database details      
    mysql_connect('hostname', 'username', 'password');
    mysql_select_db('database-name');
    
    $sql = "SELECT username FROM userregistraton";
    $result = mysql_query($sql);
    
    echo "<select name='username'>";
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";
    }
    echo "</select>";
    
    # here username is the column of my table(userregistration)
    # it works perfectly
    

    【讨论】:

    • value和文本相同时,value属性不需要声明。如果您只需要关联键,请询问 _assoc 结果集行。
    【解决方案4】:

    您需要从数据库中获取所有行,然后对其进行迭代,为每一行显示一个新的&lt;option&gt;。注意使用htmlspecialchars()避免XSS。

    $pdo = new \PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'user', 'password', [
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_EMULATE_PREPARES => false
    ]);
    
    // Select all values from the table Agents
    $stmt = $pdo->prepare("SELECT Id, Name FROM Agents");
    $stmt->execute();
    
    echo '<select name="agent" id="agent">';
    // For each row from the DB display a new <option>
    foreach ($stmt as $row) {
        // value attribute is optional if the value and the text is the same
        echo '<option value="'.htmlspecialchars($row['Id']).'">';
        echo htmlspecialchars($row['Name']); // The text to be displayed to the user
        echo '</option>';
    }
    echo '</select>';
    

    如果要预选其中一个值,则需要将selected 属性应用于&lt;options&gt; 之一:

    $selected = 'Somebody';
    
    echo '<select name="agent" id="agent">';
    foreach ($stmt as $row) {
        if ($selected === $row['Name']) {
            echo '<option value="'.htmlspecialchars($row['Id']).'" selected >';
        } else {
            echo '<option value="'.htmlspecialchars($row['Id']).'">';
        }
        echo htmlspecialchars($row['Name']);
        echo '</option>';
    }
    echo '</select>';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多