1

I am building an html form that will need to dynamically fill in the first input field with the "facility" column values in my "doctors" table. The facility column contains the names of our 7 offices. However, when I run the code below, my input field is blank and I have verified there is data in my "doctors" table. After this is working, I need to be able to dynamically fill in the second input field (which I haven't coded for in the code below because I'm stuck with the issue of first input) with the "provider" column values, also from my "doctors" table. The "provider" column contains all the provider names in our practice. However, the providers should be filtered, so that only the providers at the facility from the first input field is showing.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <?php
     $link = mysqli_connect("localhost","USERNAME","PASSWORD");
     mysqli_select_db($link,"DB");
  ?>
  <head>
    <title> Untitled Doc</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
    <form name "form1" action="" method="post">
      <select>
        <?php
        $res=mysqli_query($link,"select facility from doctors");
        while($row=mysqli_fetch_array($res)){?>
          <option> <?php echo $row ["facility"]; ?></option>
        <?php }?>
      </select>
    </form>
  </body>
</html>
NathanOliver
  • 171,901
  • 28
  • 288
  • 402

4 Answers4

1

It is important that you check if the connection have been established, as I have copied your code as it is and use it on my side and was working fine, therefore made me suspect that the problem might be connection related. also check how sensitive your server is maybe your server see this as an error : $row ["facility"] that space might be the problem as well, but it didn't on my side.

Check your server error log and also enable error reporting at the top of your page add

<?php 
ini_set('display_errors', 1); 
error_reporting(E_ALL);?>

That will enable error reporting, but use that on local server only

Then on live site send them to error log

error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);

also to get the mysqli errors, before your connection

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Important to check if your query does indeed return results before trying to display them.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <?php
     $link = mysqli_connect("localhost", "root", "", "DB");
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    ?>
    <head>
    <title> Untitled Doc</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form name ="form1" action="" method="post">
    <?php
       $query = "SELECT facility FROM doctors";

        if ($res = mysqli_query($link, $query)) {
            echo "<select name=\"myselect\">";
            while ($row = mysqli_fetch_assoc($res)) {
        ?>

        <option value="<?php echo $row['facility'];?>"><?php echo $row['facility'];?></option>
           <?php
            }

            echo "</select>";
            mysqli_free_result($res);
        } else {

            printf("Error : %s\n", mysqli_error($link));
        }

        /* close connection */
        mysqli_close($link);
        ?>



    </form>
    </body>
    </html>

NB: For your own benefit, if you haven't used prepared statements, would suggest that you learn them as well, though they are not needed in this case

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
0

When doing this kinda of query its important to catch errors so that you can debug easier. I have added a different way of connecting using mysqli object. Try this, this should determine if you have a connection error or if your query is not returning any results

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<?php
    $link = new mysqli("localhost","USERNAME","PASSWORD", "DBNAME");
    // Check connection
    if ($link->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    ?>
    <head>
        <title> Untitled Doc</title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
        <form name "form1" action="" method="post">
            <select>
            <?php

                $res = $link->query("select facility from doctors");

                // Check to see if query returned results
                if($res->num_rows > 0 {
                    while($row = $res->fetch_assoc())
                    {
                        echo '<option>' .  $row["facility"] . '</option>';
                    }
                } else {
                    echo 'No results';
                }
            ?>
            </select>

        </form>
    </body>
</html>
Chris Townsend
  • 2,586
  • 2
  • 21
  • 41
0

Based on your code, when you want to access the data in a array you should put no space between the variable and the square brackets [] or your application will not show anything. In your code when you want to echo use

<option><?php echo $row["facility"]; ?></option>

instead of

<option><?php echo $row ["facility"]; ?></option>

For another case of the second field depends on the first field, i suggest to use JavaScript to get the data by sending the first data because it will reduce the processed of getting the data.

UPDATED:

To access your data, please add another parameter in your mysqli_fetch_array to be like this

while($row = mysqli_fetch_array($res, MYSQLI_ASSOC))

this will make your data can be access using name on your table

Marprin
  • 186
  • 1
  • 6
-1
   <select>
    <?php
        $res=mysqli_query($link,"select facility from doctors");
        while($row = $result->fetch_assoc($res))
            {
    ?>
    <option> <?php echo $row ["facility"]; ?></option>
    <?php
        }   
    ?>
</select>
Ramesh S
  • 841
  • 3
  • 15
  • 35