开发者

populate dropdown is not working

开发者 https://www.devze.com 2023-04-13 07:37 出处:网络
I am trying to populate the dropdown using the following code. My database connection string works fine. But the dropdown is empty for some reason.

I am trying to populate the dropdown using the following code. My database connection string works fine. But the dropdown is empty for some reason.

<?  mysql_connect("localhost", "user", "pass")
                 mysql_select_db("mydatabase");  
$sql = "SELECT code1 FROM table1"; 
$result = mysql_query($sql) or die(mysql_开发者_运维百科error());
?>


 <p>  
<select name="vers">  
 <?php  
 while($row = mysql_fetch_array($result)){  
 echo "option value=\"".$row['code1']."\">".$row["code1"]."</option>";  
 }  
 ?>  
 </select>


Well to start with your html is invalid

echo "<option value=\"".$row['code1']."\">".$row["code1"]."</option>"; 

You're missing the opening < on the option tag.


should be

 echo "<option value=\"".$row['code1']."\">".$row["code1"]."</option>"; 


You missed one opening bracket (<) before the option value= thing


I was having the same problem, so I went online and did some research. I found a video on Youtube called "Get dropdown items from mysql database". The URL: Get dropdown items from mysql

Basically, it has you create three files:

  • a file to connect to the database
  • a file to process all your PHP
  • a file to actually display on the browser window

This follows the basic MVC (Model View Controller) file format.

After you create and connect to the database, it will show you how to write the PHP to state your query and assign it to a value to be displayed in a drop-down list.

Finally, it shows how to write the HTML to display the list with the populated data.

I would strongly suggest creating the table on your database in the video. This made it very easy for me to change what needed to be changed for my specific problem later, after I had finished writing the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消