dongxi8297 2015-07-10 23:45
浏览 59
已采纳

在mysqli生成的下拉框中的一行上显示多个值

Using mysqli code, I have been able to create a dropdown box using data from a mysql database. The sql statement selects the userId, f_Name, and l_Name from the database. This list is part of a form and when the user selects the name, I want to pass only the userId via the $_Post method. How can that be done? Thanks again for your help.

<select name="names">
    <option value = "">---Select---</option>
    <?php
        $queryusers = 'SELECT userID, f_Name, l_Name FROM users';
        $db = mysqli_query($mysqli, $queryusers);
        while ( $names=mysqli_fetch_assoc($db)) {
           echo "<option value='{".$names['f_Name']."}' . 
                {".$names['l_Name']."}'>".$names['f_Name']. " " .
                $names['l_Name']."</option>";
        }
    ?>
</select>
  • 写回答

1条回答 默认 最新

  • doudan1123 2015-07-10 23:48
    关注

    Just do something like this:

    echo '<option value="'.$names['userID'].'" >'.$names['f_Name'].' '. $names['last_nm'] . '</option>';
    

    that becomes the value of the option, which is what gets submitted.

    Edit

    This is your code using this

    <select name="names">
        <option value = "">---Select---</option>
        <?php
            $queryusers = 'SELECT userID, f_Name, l_Name FROM users';
            $db = mysqli_query($mysqli, $queryusers);
            while ( $names=mysqli_fetch_assoc($db)) {
               echo '<option value="'.$names['userID'].
                '" >'.$names['f_Name'].' '. $names['last_nm'] . '</option>';
            }
        ?>
    </select>
    

    another edit to show the mysqli prepared statement example

    Assuming you are inserting these same values into a table, you'd just do it like this:

    Your mysqli connection would be something like this:

    $conn = new mysqli($host, $username, $password, $dbname)
    

    Then you'd do your insert like this (assuming userID is an autoincrement key)

    $stmt = $dbc->prepare("insert into users (userID, f_name, L_name)
                           values (NULL, ?, ?)");
    $stmt->bind_param("ss", $names['f_name'],$names['l_name'])
    $stmt->execute();
    $stmt->close();
    

    The "ss" just means both variables are strings; if they are integers you'd use i. If you are doing an update, you'd just replace any variables you'd use in the insert with those ? placeholders. It works the same way.

    Here's the manual: mysqli prepared statements

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改