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>
14
  • This question needs more details. Are you seeing any errors? are you consoling out the results of $row? What are the results of $row? Commented Apr 6, 2017 at 14:05
  • try removing the space $row ["facility"]; should be $row["facility"]; Also, Try to do print_r($row); Commented Apr 6, 2017 at 14:06
  • try this while($row = $result->fetch_assoc()) Commented Apr 6, 2017 at 14:07
  • @RameshS no this is procedural, the db con must be first parameter Commented Apr 6, 2017 at 14:10
  • sorry am wrongly post @MasivuyeCokile Commented Apr 6, 2017 at 14:11

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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>

6 Comments

also suspect that he's connection is the problem
@Chris Townsend- you have a space between $row and ["facility"] which shouldn't be there :). Probably you copied from the question like that..
Thanks @MarinNedea
I also copied like that and changed the connection only, I still have the space and my dropdown is working well @MarinNedea
If the mysql connection doesn't connect, I do not believe it throws any exceptions. The only errors you'll get is from syntax errors or undefined variables
|
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

3 Comments

I have just copied his code as it is and ran on my localhost with that space and it work
@MasivuyeCokile, I just updated my answer. You can try again. Thanks.
this is not my question
-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>

3 Comments

any explanation?
mysqli_fetch_assoc() function is used to return an associative array representing the next row in the res
You asking the wrong person.. you still have not explained why he should use fetch_assoc() instead of mysqli_fetch_array() The downvote is not mine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.