Navbar

My Blog List

Tuesday, September 8, 2020

Part 3-Admin Panel(Registration): How to Fetch Data from Database in php


As per the video, we have already downloaded the files and linked the CSS and JS.


Step 1: Create a page register.php and paste the below code

<?php

include('security.php');

?>

<div class="container-fluid">

<div class="card shadow mb-4">

  <div class="card-header py-3">

    <h6 class="m-0 font-weight-bold text-primary">Admin Profile</h6>

  </div>

  <div class="card-body">


    <div class="table-responsive">

    <?php


        $query = "SELECT * FROM register";

        $query_run = mysqli_query($connection, $query);

        

    ?>

      <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">

        <thead>

          <tr>

              <th> ID </th>

              <th> Username </th>

              <th>Email </th>

              <th>Password</th>

              <th>UserType</th>

              <th>EDIT</th>

              <th>DELETE</th>

          </tr>

        </thead>

        <tbody>

        <?php

        if(mysqli_num_rows($query_run) > 0)        

        {

            while($row = mysqli_fetch_assoc($query_run))

            {

               ?>

          <tr>

            <td><?php  echo $row['id']; ?></td>

            <td><?php  echo $row['username']; ?></td>

            <td><?php  echo $row['email']; ?></td>

            <td><?php  echo $row['password']; ?></td>

            <td><?php  echo $row['usertype']; ?></td>

            <td>

                <form action="register_edit.php" method="post">

                    <input type="hidden" name="edit_id" value="<?php echo $row['id']; ?>">

                    <button  type="submit" name="edit_btn" class="btn btn-success"> EDIT</button>

                </form>

            </td>

            <td>

                <form action="code.php" method="post">

                  <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">

                  <button type="submit" name="delete_btn" class="btn btn-danger"> DELETE</button>

                </form>

            </td>

          </tr>

          <?php

            } 

        }

        else {

            echo "No Record Found";

        }

        ?>

        </tbody>

      </table>

   

    </div>

  </div>

</div>


</div>

<!-- /.container-fluid -->


Step 2: Create a folder called database and then create a file named dbconfig.php and paste the below code.


<?php


$server_name = "localhost";

$db_username = "root";

$db_password = "";

$db_name = "adminpanel";


$connection = mysqli_connect($server_name,$db_username,$db_password,$db_name);


if(!$connection)

{

    die("Connection failed: " . mysqli_connect_error());

    echo '

        <div class="container">

            <div class="row">

                <div class="col-md-8 mr-auto ml-auto text-center py-5 mt-5">

                    <div class="card">

                        <div class="card-body">

                            <h1 class="card-title bg-danger text-white"> Database Connection Failed </h1>

                            <h2 class="card-title"> Database Failure</h2>

                            <p class="card-text"> Please Check Your Database Connection.</p>

                            <a href="#" class="btn btn-primary">:( </a>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    ';

}

?>


Step 3: Create a page security.php and paste the below code.


<?php

session_start();

include('database/dbconfig.php');


if($connection)

{

    // echo "Database Connected";

}

else

{

    header("Location: database/dbconfig.php");

}

?>

1 comment: