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>";
Edited by seany123
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>

Edited by mattm_14
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);
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.