通过具有特定条件的SQL填充下拉列表

时间:2018-05-20 23:04:00

标签: php html sql

基本上,我正在尝试从我的表中选择其type ='users'的所有用户,如果满足该条件,请选择他们的名字和姓氏。将它们存储到变量中,然后将变量逐个显示在一个下拉列表中(数组)。

当前代码:

 <div id="ContainerC">
        <form action="AppointmentAddQuery.php" method="post">
            <input type="text" name="date" class="datepicker" placeholder="Please select a date" required><br>
            <input type="text" name="patient" class="textbox" placeholder="Patient" required> <br>

            <?php
            include 'dbconnection.php';



            ?>



                <input type="text" name="phlebotomist" class="textbox" placeholder="phlebotomist" required> <br>


                <input type="text" name="bloods" class="textbox" placeholder="blood required" required><br>
                <p>Has the appointment been allocated?</p>
                <select name="allocated" class="textbox" required>
      <option value="yes">yes</option>
      <option value="no">no</option>
   </select>


                <br><br><br>

                <button type="submit" name="submit" class="SUB">Add patient</button>
        </form>
    </div>

SQL setup:

2 个答案:

答案 0 :(得分:0)

对于问题的SQL部分,您可能正在寻找类似的内容:

select firstname
       , lastname
from <table>
where type = 'user'

答案 1 :(得分:0)

假设您的数据库结构(您可能需要替换表名和列名),您可以这样做:

<?php 
    include 'dbconnection.php';
    $sql = "SELECT firstname, lastname FROM users WHERE type = 'users'";
    $result = $conn->query($sql) or die($conn->error);

    if ($result->num_rows > 0) {
        echo '<select>';
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo '<option value="'.$row["firstname"].$row["lastname"].'">'.$row["firstname"].$row["lastname"].'</option>';
        }
        echo '</select>';
    } else {
        echo '<select><option value="">No Users</option></select>';
    }
    $conn->close();
?>

这也假设在dbconnection.php中你创建了一个像mysqli这样的对象:

$conn = new mysqli($servername, $username, $password, $dbname);