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
https://forums.phpfreaks.com/topic/71656-solved-auto-populating-a-drop-down-box/
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)) {  

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.