-1

Essentially, i'm trying to select all the users from my table where their type ='users' and if that condition is met, select their first and last name. Store them into variables and then display the variables into a dropdown list one by one(array).

Current code:

 <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:

  • So, whats your question? – LordBaconPants May 20 '18 at 23:05
  • How would I go about selecting certains values from the database through the SQL commands and then display them into an array for the drop downlist? @LordBaconPants – EvilChewwie May 20 '18 at 23:06
  • Something along the lines of https://stackoverflow.com/questions/5189662/populate-a-drop-down-box-from-a-mysql-table-in-php ? – LordBaconPants May 20 '18 at 23:12
  • @LordBaconPants Something simular however I only want values from the database that come from the type="user", if that condition is met then collect the firstname and secondname data – EvilChewwie May 20 '18 at 23:14

2 Answers2

0

For the SQL part of your question, you are probably looking for something like:

select firstname
       , lastname
from <table>
where type = 'user'
LordBaconPants
  • 1,404
  • 1
  • 19
  • 22
0

Assuming the structure of your database (you will likely need to replace table and column names) this is how you could do it:

<?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();
?>

This also assumes that inside of dbconnection.php you created a mysqli object like so:

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

  • Unfortunately when trying your code, it simply displays part of the code inside the dropdown list – EvilChewwie May 20 '18 at 23:56
  • My mistake, there was a missing single quote at the end the looping echo. I just tested the code on a local db and it works. – Tim Schorr May 21 '18 at 00:45