Jump to content

populate drop down with PHP


mattm_14

Recommended Posts

Hi

 

I am completely new to PHP and I am looking to populate a drop down box on my page with values from my database (list of venues).

 

I have got the following code but nothing happens. I would really appreciate it if someone could assist?

 

<select name="venue">
            
         <?php

            $user = 'root';
            $pass = 'admin';
            $db = 'r_db';

            $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");

            $query = "SELECT Venue FROM venue_table";

            $response = @mysqli_query($db,$query);
                
                $venue.="";
                
                while($row = mysqli_fetch_array($response)){
                
                
                $venue= "<option>" . $row['Venue']. "</option>",
                    
                            
            }
                echo $venue;
            ?>
        
        
        </select>

Link to comment
Share on other sites

Hi

 

I am completely new to PHP and I am looking to populate a drop down box on my page with values from my database (list of venues).

 

I have got the following code but nothing happens. I would really appreciate it if someone could assist?

 

<select name="venue">

            

         <?php

 

            $user = 'root';

            $pass = 'admin';

            $db = 'r_db';

 

            $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");

 

            $query = "SELECT Venue FROM venue_table";

 

            $response = @mysqli_query($db,$query);

                

                $venue.="";

                

                while($row = mysqli_fetch_array($response)){

                

                

                $venue= "<option>" . $row['Venue']. "</option>",

                    

                            

            }

                echo $venue;

            ?>

        

        

        </select>

 

so first, you need to have a semicolon after assigning the variable, secondly you would want to append the values to the variable $venue rather than overwriting it on every run of the while loop...

$venue = $venue . "<option>" . $row['Venue']. "</option>";
Link to comment
Share on other sites

thank you seany123, but I still don't get any values. I have adjusted to this:

 

<select name="going">
            <option value="0">Please Select</option>
            <?php
            
             $user = 'root';
            $pass = 'admin';
            $db = 'racing_database';

            $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");

            $query = "SELECT Venue FROM venue_table";

            $response = @mysqli_query($db,$query);
                        
                    $venue="";
                
                while($row = mysqli_fetch_array($response)){
                                
                $venue = $venue . "<option>" . $row['Venue']. "</option>";
                            
                
            }
                echo $venue;
            ?>
            
          </select>

Link to comment
Share on other sites

thank you seany123, but I still don't get any values. I have adjusted to this:

 

<select name="going">

            <option value="0">Please Select</option>

            <?php

            

             $user = 'root';

            $pass = 'admin';

            $db = 'racing_database';

 

            $db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");

 

            $query = "SELECT Venue FROM venue_table";

 

            $response = @mysqli_query($db,$query);

                        

                    $venue="";

                

                while($row = mysqli_fetch_array($response)){

                                

                $venue = $venue . "<option>" . $row['Venue']. "</option>";

                            

                

            }

                echo $venue;

            ?>

            

          </select>

 

is the column called 'Venue' with a capital v in the sql table?

Link to comment
Share on other sites

^^^ it's even worse that that (thanks php.net.) when using OOP notation for the connection, an object is always returned and $db will always be an object (true) value. so, three things - 1) the or die(...) logic won't ever be triggered, 2) if you are using program logic to detect a connection error, you must use mysqli_connect_error() (or the ->connect_error property, assuming you have a php version where that works), and 3) if you can, switch to use the php PDO extension. it is much better implemented, simpler to use, and more constant than the php mysqli extension.

 

avoid all the problems with handling connection and query errors by enabling exceptions, then simply let php catch the exception and use its error_reporting/display_errors/log_errors settings to control what happens with the actual error information. you can then remove (which currently isn't working anyways) or leave out any error checking logic in your code.

 

to enable exceptions for the php mysqli extension, simply add the following line before the point where you are making the database connection - 

mysqli_report(MYSQLI_REPORT_ALL);
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.