Display HTML List With MySQLi Using PHP

This code will tackle about Display HTML List With MySQLi using php. The program can insert data and retrieve it from database as a HTML list. You can use this code if you want to display hyperlinks in your site. To learn more about this tutorial, just follow the step below.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP https://www.apachefriends.org/index.html And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/

Creating Database

Open your database web server then create a database name in it db_html_list after that click Import then locate the database file inside the folder of the application then click ok. tut

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_html_list");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating the Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. <div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Display HTML List With MySQLi Using PHP</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-9">
  18. <h3>PROGRAMMING LANGUAGES LIST</h3>
  19. <ul>
  20. <?php
  21. require'conn.php';
  22.  
  23. $query=mysqli_query($conn, "SELECT * FROM `list`") or die(mysqli_error());
  24. while($fetch=mysqli_fetch_array($query)){
  25. echo "<li>".$fetch['list']."</li>";
  26. }
  27. ?>
  28. </ul>
  29. </div>
  30. <div class="col-md-3">
  31. <form method="POST" action="insert.php">
  32. <div class="form-group">
  33. <label>Enter here..</label>
  34. <input type="text" name="list" class="form-control" required="required"/>
  35. </div>
  36. <center><button class="btn btn-primary" name="insert">Insert</button></center>
  37. </form>
  38. </div>
  39. </div>
  40. </body>
  41. </html>

Creating Main Function

This code contains the main function of the application. The code will display the list of data from MySQLi database. To make this just copy and write these block of codes inside the text editor, then save it as insert.php
  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_POST['insert'])){
  5. $list=$_POST['list'];
  6.  
  7. mysqli_query($conn, "INSERT INTO `list` VALUES('', '$list')") or die(mysqli_error());
  8.  
  9. header('location: index.php');
  10. }
  11. ?>
There you have it we successfully created Display HTML List With MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment