Populate Drop Down List From Database Using PHP and MySQLi

0
576
Populating Drop Down List with PHP
Populate Drop Down List From Database Using PHP and MySQLi

In this tutorial, I will teach you how to Populate Drop Down List From Database Using PHP and MySQLi. This method has the ability to fill the data in a drop down list form MySQL database.

Follow the step by step procedure in Populating Drop Down List From Database Using PHP and MySQLi below.

Lets Begin:

First, create a landing page and name it “index.php“.

Second, do the following code in the page that you have created.

 

  1. </div>
  2. <div>&lt;html&gt;
  3. &lt;!-- Put you mysqli connection here ................ --&gt;
  4. &lt;title&gt;Populate Drop Down List From Database Using PHP and MySQLi&lt;/title&gt;
  5. &lt;header&gt;
  6. &lt;h1 align="center"&gt;Populate Drop Down List From Database Using PHP and MySQLi&lt;/h1&gt;
  7. &lt;/header&gt;
  8. &lt;!-- Put your style layout here...... --&gt;
  9. &lt;body&gt;
  10. &lt;div id="box"&gt;
  11. &lt;div class="column"&gt;&lt;/div&gt;
  12. &lt;div class="column"&gt;
  13. &lt;div class="clear"&gt;&lt;/div&gt;
  14. &lt;label class="first-column "&gt;Select a Book:&lt;/label&gt;&lt;div class="clear"&gt;&lt;/div&gt;
  15. &lt;div class="clear"&gt;&lt;/div&gt;
  16. &lt;/div&gt;
  17. &lt;div class="column"&gt;&lt;/div&gt;
  18. &lt;/div&gt;
  19. &lt;/body&gt;
  20. &lt;/html&gt;</div>
  21. <div>
Third, create a css style layout of the page.
 

  1. </div>
  2. <div>#box{
  3. width: 100%;
  4. }
  5. .column {
  6. width: 33.33%;
  7. display: inline-block;
  8. }
  9. .form{
  10. padding: 2px;
  11. width: 100%;
  12. display: inline-block;
  13. }
  14. .first-column {
  15. display: inline-block;
  16. width: 100px;
  17. height: 2px;
  18. margin: 2px;
  19. position: inherit;
  20. }
  21. .second-column{
  22. display: inline-block;
  23. width: 150px;
  24. height: 2px;
  25. margin: 2px;
  26. position: inherit;
  27. }
  28. select {
  29. width: 150px;
  30. font-size: 12px;
  31. height: 25px;
  32. padding: 4px
  33. }
  34. .clear{
  35. clear: left;
  36. height: 15px;
  37. }</div>
  38. <div>
Fourth, create a connection between MySQL Database and PHP Script.
  1. </div>
  2. <div>&lt;?php
  3. $server="localhost";
  4. $userid ="root";
  5. $Password = "";
  6. $myDB = "booksdb";$con = mysqli_connect($server,$userid,$Password,$myDB);if (mysqli_connect_errno()) {
  7. # code...
  8. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  9. }
  10.  
  11. ?&gt;
  12.  
  13. </div>
  14. <div>
Finally, create a function for retrieving data from the database and fill it into the select box.
  1. </div>
  2. <div>
  3.  
  4. &lt;?php
  5.  
  6. echo '&lt;select&gt;
  7. &lt;option&gt;Select&lt;/option&gt;';
  8.  
  9. $sqli = "SELECT * FROM tblbooks";
  10. $result = mysqli_query($con, $sqli);
  11. while ($row = mysqli_fetch_array($result)) {
  12. echo '&lt;option&gt;'.$row['BookTitle'].'&lt;/option&gt;';
  13. }
  14.  
  15. echo '&lt;/select&gt;';
  16.  
  17. ?&gt;
  18.  
  19. </div>
  20. <div>

If you have any questions or suggestion about Populate Drop Down List From Database Using PHP and MySQLi, please feel free to contact us at our contact page.You can subscribe this site to see more of my tutorials.

Download the Populate Drop Down List From Database Using PHP and MySQLi complete source code  here.

 

Facebook Comments
(Visited 639 times, 6 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here