如何从选择表单输入中显示的值发布不同的值

问题描述 投票:2回答:1

在我的网页上,我有以下表格。我使用一些PHP来显示选择输入中的用户名。我的问题是这个...如何将id列(我在SQL中选择)保存到数据库而不是我正在显示的名称列?所有帮助非常感谢。

<form action='insertmodule.php' method='POST'>
                <span class="form-group">
                    <input type="text" placeholder="Name" class="form-control" name="name" required/>
                </span>
                <br>
                <div class="form-group">
                   <label for="exampleSelect1">Select the tutor for the module</label>
                    <select class="form-control" name="tutor">
                      <?php
                        include('../connections/conn.php');
                        $sql = mysqli_query($conn, "SELECT id, first_name, last_name FROM cater_users WHERE role = 2");
                        while ($row = $sql->fetch_assoc()){
                        echo "<option>" . $row['first_name'] . " ". $row['last_name'] . "</option>";
                        }
                      ?>
                    </select>
                 </div>


                <button type="submit" class="btn btn-primary" name='sign'>Create</button>
            </form>
php html forms
1个回答
3
投票

试试这个

echo sprintf('<option value="%s">%s %s</option>', $row['id'], $row['first_name'], $row['last_name']);
© www.soinside.com 2019 - 2024. All rights reserved.