Jump to content

[SOLVED] Auto populating a drop down box


RyanW67

Recommended Posts

Hi All,

 

I've searched the net for examples and have really hit a stump, I'm quite a newbie to MySQL so would really appreciate it if any of you guys could help...

 

Basically I'm setting up a website which needs an populated drop down box made up from all fields in a specific column of a table in a mysql db....

 

Here's the code I've made up using various tutorials....

<?php 
$user = "";  
$host = "" 
$password = "" 
$dbName =  "" 

/* make connection to database */  
mysql_connect($host, $user, $password) OR DIE( "Unable to connect 
to database"); 
mysql_select_db($dbName); //did you forget this line? 

$sql = "SELECT model FROM usedVehicles"; 
$query = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT 
?> 

<form action="action" method="post">  
<select name="option">  

<?php  
while ($row = mysql_fetch_array($result)) {  
    echo "<option value=\"" . $row['model'] . "\">" . $row['model'] . "</option>\n";  
}  
?>  
</select>  
<input type="submit">  
</form>

 

 

I've left out the connection details for obvious reasons...

 

When I upload and try to test this, jus a blank drop down appears... there are definately fields in the column as I have tried the query on phpMyAdmin...

 

All help is really appreciated.

 

Ryan

 

Link to comment
Share on other sites

The $query:

$query = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT 

should be $result..

 

$result = mysql_query($sql) or die(mysql_error() . "<br>$sql"); //use the or die(...) part ONLY IN DEVELOPMENT 

 

that is what you use in the while loop:

 

while ($row = mysql_fetch_array($result)) {  

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.