Navbar

My Blog List

Wednesday, September 16, 2020

PHP CRUD-2 : Bootstrap Modal (POP UP): Fetch data from Database in html table in PHP


Here, in this above video, we are learning about how to fetch the record from table named student from database in a html table format using php scriptring language.

So, now we will start the process by following the below code and steps:

Step 1: Create a table named students :


As you have already created this table named student in your database like below columns:
1. id (PrimaryKey, AI)
2. fname
3. lname
4. class
5. section

so, now you will be having some data in your table: 

Step 2: Create a file named index.php  and paste the below code init:


We are working on this for fetching the data in html table format using php, in details we are using bootstrap-4 for designing the table and then fetch the records. to perform this task, copy and paste the below code.

We are using php code to fetch the record from database,
1. database connection is done.
2. MySqli query is done.
3. PHP Scripting is done.

//------------------------------------------------------------------------
<?php session_start(); ?> <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> <title>PHP CRUD - Bootstrap Modal (POP UP)</title> </head> <body> <div class="container mt-5"> <div class="row"> <div class="col-md-12"> <div class="card"> <?php if(isset($_SESSION['status']) && $_SESSION['status'] !='') { ?> <div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>Hey!</strong> <?php echo $_SESSION['status']; ?> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <?php unset($_SESSION['status']); } ?> <div class="card-header"> <h5> PHP CRUD - Bootstrap Modal (POP UP) <!-- Button trigger modal --> <button type="button" class="btn btn-primary float-right" data-toggle="modal" data-target="#studentModal"> Add Student </button> </h5> </div> <div class="card-body"> <table class="table"> <thead> <tr> <th scope="col">#ID</th> <th scope="col">First Name</th> <th scope="col">Last Name</th> <th scope="col">Class</th> <th scope="col">Section</th> <th scope="col">Action</th> </tr> </thead> <tbody> <?php $conn = mysqli_connect("localhost","root","","phpcrud"); $query = "SELECT * FROM students"; $query_run = mysqli_query($conn, $query); if(mysqli_num_rows($query_run) > 0) { // while($row = mysqli_fetch_array($query_run)) foreach($query_run as $row) { ?> <tr> <td class="stud_id"><?php echo $row['id']; ?></td> <td><?php echo $row['fname']; ?></td> <td><?php echo $row['lname']; ?></td> <td><?php echo $row['class']; ?></td> <td><?php echo $row['section']; ?></td> <td> <a href="#" class="badge badge-primary">VIEW</a> <a href="#" class="badge badge-info">EDIT</a> <a href="#" class="badge badge-danger">DELETE</a> </td> </tr> <?php } } else { echo "<h5>No Record Found</h5>"; } ?> </tbody> </table> </div> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script> </body> </html>
//------------------------------------------------------------------------

No comments:

Post a Comment