For making select options from mysql database table, we are creating a table with name "country" in mysql database to show countires as options. And filling some countries as given below in the country table.
use database infosearch; CREATE TABLE country ( id int (10) PRIMARY KEY AUTO_INCREMENT, name varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; INSERT INTO country (name) VALUES('Afghanistan'); INSERT INTO country (name) VALUES('Albanie'); INSERT INTO country (name) VALUES('Bahamas'); INSERT INTO country (name) VALUES('Bangladesh'); INSERT INTO country (name) VALUES('Cambodia'); INSERT INTO country (name) VALUES('Colombia'); INSERT INTO country (name) VALUES('Egypt'); INSERT INTO country (name) VALUES('Georgia'); INSERT INTO country (name) VALUES('Germany'); INSERT INTO country (name) VALUES('India'); INSERT INTO country (name) VALUES('Ireland');After creating a table and filling values, we are going to make function, which returns an array with id and countries names from mysql table;
<?php function getCountries(){ $host = "localhost"; $username = "user_name"; $password = "password"; $database = "database_name"; $items = array(); $link = new mysqli($host,$username,$password,$database); if ($link->connect_error) { die('Connection error: ' . $link->connect_error); } else { $query = "SELECT * FROM country"; $result = $link->query($query); while ($row = $result->fetch_assoc()) { $items[$row['id']]=$row['name']; } $link->close(); } return $items; } //Our next step is making HTML form, So make form as given. ?><form name="form1" action="simple_fom_result.php" method="post"> <table> <tr><td>First Name</td><td><input type="text" name="first_name" id="first_name" /></td></tr> <tr><td>Last Name</td><td><input type="text" name="last_name" id="last_name" /></td></tr> <tr><td>Gender </td> <td> <input type="radio" name="gender" id="gernder-1" value="male" checked="checked" /> Male <input type="radio" name="gender" id="gernder-2" value="female" /> Female
</td></tr> <tr><td>Country </td><td> <select name="country" id="country" > <?php $options=getCountries(); foreach($options as $key=>$val){ ?> <option value="<?php echo $key ?>"><?php echo $val?></option> <?php } ?> </select> </td></tr> <tr><td>Your Hobbies </td><td> <textarea name="hobbies" id="hobbies" rows="5" cols="36"></textarea> </td></tr> <tr><td></td><td><input type="submit" name="btn" id="btn" value="Submit" /></td></tr> </table> </form>