0

In my HTML web page I want to make my dropdown menu pull options from a MySQL database, so I embedded PHP inside. However, when you select the drop down it is saying '.$id.' literally rather than a name that it should.

<label>Test Dropdown</label>
<select class="form-control" name="client">
    <option value="pick">CHOOSE</option>
    <?php
        $dbhost = '#########';
        $dbuser = '#########';
        $dbpass = '$$$$$$$$$$$$$$';
        $dbdata = '############';
        $conn = mysql_connect($dbhost, $dbuser, $dbpass);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT * FROM client";
        mysql_select_db('######');
        $result = mysql_query($sql, $conn);

        if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                $id = $row['fname'];
                echo '<option value="'.$id.'">'.$id.'</option>';
            }
        }
    ?>
</select>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

0

You are getting that output i.e .$id. that's because you have put it into inverted comma, so it is taking it as an string rather than variable so try this:

 echo '<option value="'.$id.'">.$id.</option>';

instead of

echo '<option value="'.$id.'">'.$id.'</option>';
Ijhar Ansari
  • 310
  • 1
  • 2
  • 9
0

If your website can serve actual .php files, but the php code doesn't execute in your .html or .htm , chances are you need to add php support at the webserver level.

  • Most hosting plans are usually on a LAMP stack and support PHP. If you have such a hosting plan, this can be done using an .htaccess file or through the CPanel/Plesk interface. Refer to this blog post that explains how to test if php works on your server and get it working within .html or .htm files.

  • However, if you have a custom webserver or an IIS server, you may need to install and configure PHP support before you can even enable it for html files.)

Anson W Han
  • 409
  • 2
  • 7