Friday, May 21, 2010

How to code a simple php dynamic dropdown form feild from mysql?

I been struggling with this all morning.


I am tiring to create a dynamic form field that uses data from a myself database but cannot seem to get it to work.


Anyone know how to code a simple php code for a single dropdown field form that pulls data from a table field on a mysql database.

How to code a simple php dynamic dropdown form feild from mysql?
You have a normal drop down option box, and for each line to insert the fields returned from your query.





When you say dynamic, you won't be able to do more fetches after the page has loaded. You only get one chance to do the PHP.
Reply:Here's my code:


%26lt;?php


//please replace the variable with its respective values


//$host for the mysql host name


//$username for the mysql user name


//$password for the mysql password


mysql_connect($host,$username,$passwo...


//$dbname for the database name


mysql_select_db($dbname);


?%26gt;


%26lt;form name="form1" method="post" action=""%26gt;


%26lt;?php


$query = "SELECT * FROM country";


$result=mysql_query($query);


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


echo'%26lt;select name="'.$row['name'].'"%26gt;%26lt;/select%26gt;';


}


?%26gt;


%26lt;/form%26gt;





So what Im doing in my code is first connect to the database and select which database im going to use. And then on the second part of the php im retrieving the 'name' field from 'Country' table and print it.
Reply:Contact me and I will send you some sample code


No comments:

Post a Comment