How can I display data from MYSQL database using HTML table from a PHP file?

BaselE

Using the PHP code below I get two separate tables for each row in MYSQL database table. How can I get the code to display the data in one table? Also for some reason the CSS is not getting applied to some parts of the PHP file where I have links to edit and remove data, and the background color of the table row. Should I apply the CSS directly in the PHP file? or is there any other way to do it?

<?php
    session_start();
    mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_INDEX);
    $initials=parse_ini_file("../.ht.asetukset.ini");
    try {
        $connection=mysqli_connect($initials["databaseserver"], 
                                   $initials["username"],
                                   $initials["password"],
                                   $initials["database"]
                                   );
    } catch (Exception $e) {
        header("Location:../html/yhteysvirhe.html");
        exit;
    }
?>
<?php
    include "../html/admin_header.html";
?>
 <?php
 //getting result from database
 $print=mysqli_query($connection, "SELECT * FROM users");
 while ($row=mysqli_fetch_object($print)) {
 echo "<table>";
    echo "<tr>";
    echo "<th>ID</th>";
    echo "<th>First Name</th>";
    echo "<th>last Name</th>";
    echo "<th>User Name</th>";
    echo "<th>Email</th>";
    echo "<th>Password</th>";
    echo "<th>Description</th>";
    echo "<th>Delete</th>";
    echo "<th>Edit</th>";
    echo "</tr>";
    echo "<tr>";
    echo "<tr>";
    echo "<td><h2>$row->fname</h2></td>";
    echo "<td><h2>$row->lname</h2></td>";
    echo "<td><h2>$row->uname</h2></td>";
    echo "<td><h2>$row->email</h2></td>";
    echo "<td><h2>$row->paswd</h2></td>";
    echo "<td><h2>$row->descrip</h2></td>";
    echo "<td><h2><a style='color:white' href='./admin_remove.php?poistettava=$row->id'>Delete</a></h2></td>";
    echo "<td><h2<a href='./admin_edit.php?editable=$row->id'>Edit</a></h2></td>";
 }
    echo "</tr>";
    echo "</table>";

    mysqli_close($connection);
?>

<?php
    include "../html/admin_footer.html";
?>

This is the result I get:

/*body background color.*/

body {
  background-color: #2f303a;
}


/*Removing underline from hyper
links.*/

a {
  text-decoration: none;
  color: #1d19f6;
}


/*navbar bakground color change.
*/

.navbar {
  background-color: #c4c4c4;
}


/* Spaces between items are increased
in the navbar.*/

.navbar-brand {
  padding-left: 40px;
}


/* Spaces between items are increased
in the navbar.*/

.nav-item {
  padding-left: 80px;
  padding-right: 80px;
}


/**/

.nav-link:hover {
  background-color: #eeea14;
  border-radius: 25px;
}

a:hover {
  color: #00ff6a;
}


/* Search bar with rounded corners
and white & blue background.*/

.navbar1 {
  background-color: #0000ff;
  border-radius: 25px;
}


/* Search bar with rounded corners
and white & blue background.*/

.btn {
  color: #ffffff;
  border: none;
  border-radius: 25px;
}


/* Search bar with rounded corners
and white & blue background.*/

.form-control1 {
  color: #555555;
  border: none;
  border-radius: 25px;
  padding-left: 20px;
  width: 299px;
  margin-left: -20px;
}


/*box config.*/

.box {
  background-color: #ffffffea;
  width: auto;
  height: 556px;
  position: relative;
  flex-direction: column;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}


/*can be used to add spacing between 
items stacked on top of each other.*/

.padding {
  padding-bottom: 1%;
}


/*container.*/

.container {
  width: auto;
  height: auto;
  position: static;
  flex-direction: row;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-content: space-around;
  align-items: center;
}

.sec {
  background-color: #ffffffea;
  width: auto;
  height: 556px;
  position: relative;
  flex-direction: row;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  background-color: #2f303a;
  color: #ffffff;
  text-decoration: none;
}

.sec1 {
  padding-top: 1%;
  background-color: #ffffffea;
  width: auto;
  height: auto;
  position: relative;
  flex-direction: row;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  background-color: #2f303a;
  color: #ffffff;
}


/*white boxes config.*/

.box1 {
  border-radius: 30px;
  background-color: #ffffff;
  color: #1d19f6;
  width: 358px;
  height: 293px;
  position: relative;
  flex-direction: row;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  margin-top: 20%;
  margin-bottom: 20%;
  text-shadow: 2px 2px 4px #000000;
  box-shadow: 10px 10px 5px #494848;
}

.box2 {
  width: auto;
  display: flex;
  background-color: #000000;
  padding: 15px 15px 15px 15px;
}

table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td,
th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: blue;
}


/*footer config*/

footer {
  display: flex;
  flex-flow: column wrap;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  background-color: #2f303a;
  padding: 15px 15px 15px 15px;
  color: #ffffff;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="author" content="">
  <meta name="description" content="Team 14 project work. The website contains basic web development guides utilizing HTML5, CSS, JavaScript and Bootstrap">
  <title>admin</title>
  <!--Link to bootstrap.-->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <link rel="stylesheet" type="text/css" href="../css/style.css">
  <!--Link to local css file.-->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Dosis&display=swap" rel="stylesheet">
</head>

<body>
  <header>
    <!--navigation bar starts here.-->
    <nav class="navbar navbar-expand-lg navbar-light">
      <div class="container-fluid">
        <a class="navbar-brand" href="index.html" style="color: #0000ff; font-family:'Dosis', sans-serif; font-size: 20px;"><b>CodeSchool</b></a>
        <div id="navbarNav">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link active" aria-current="page" href="./html.html">HTML Basics</a>
            </li>
            <li class="nav-item">
              <a class="nav-link active" href="./css.html">CSS Basics</a>
            </li>
            <li class="nav-item">
              <a class="nav-link active" href="./js.html">JavaScript</a>
            </li>
            <li class="nav-item">
              <!--anchor link added.-->
              <a class="nav-link active" href="#a">About Us</a>
            </li>
            <li class="nav-item">
              <!--anchor link added.-->
              <a class="nav-link active" href="#b">Contact Us</a>
            </li>
            <li class="nav-item">
              <!--anchor link added.-->
              <a class="nav-link active" href="#">Log In / Sgin Up</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
  </header>
  <!--navigation bar ends here.-->
  <main>
    <!--box with search bar starts here.-->
    <div class="box">
      <h1 class="padding"><b>Welcome to the Admin Page</b></h1><br>
      <h1 class="padding"><b>Here you can edit users' data and/or remove profiles from the database</b></h1><br>
    </div>
    <!--box with search bar ends here.-->
    <!--other content.-->
    <div class="sec1">
      <h1 class="padding"><b>Users list:</b></h1><br>
    </div>
    <div class="sec">
      <table>
        <tr>
          <th>ID</th>
          <th>First Name</th>
          <th>last Name</th>
          <th>User Name</th>
          <th>Email</th>
          <th>Password</th>
          <th>Description</th>
          <th>Delete</th>
          <th>Edit</th>
        </tr>
        <tr>
          <tr>
            <td>
              <h2>1</h2>
            </td>
            <td>
              <h2>Admin</h2>
            </td>
            <td>
              <h2>Admin</h2>
            </td>
            <td>
              <h2>admin</h2>
            </td>
            <td>
              <h2></h2>
            </td>
            <td>
              <h2>password</h2>
            </td>
            <td>
              <h2>Adiminstrator</h2>
            </td>
            <td>
              <h2><a style='color:white' href='./admin_remove.php?poistettava=1'>Delete</a></h2>
            </td>
            <td>
              <h2<a href='./admin_edit.php?editable=1'>Edit</a>
                </h2>
            </td>
            <table>
              <tr>
                <th>ID</th>
                <th>First Name</th>
                <th>last Name</th>
                <th>User Name</th>
                <th>Email</th>
                <th>Password</th>
                <th>Description</th>
                <th>Delete</th>
                <th>Edit</th>
              </tr>
              <tr>
                <tr>
                  <td>
                    <h2>2</h2>
                  </td>
                  <td>
                    <h2>test</h2>
                  </td>
                  <td>
                    <h2>test</h2>
                  </td>
                  <td>
                    <h2>tester</h2>
                  </td>
                  <td>
                    <h2>[email protected]</h2>
                  </td>
                  <td>
                    <h2>test</h2>
                  </td>
                  <td>
                    <h2>testing</h2>
                  </td>
                  <td>
                    <h2><a style='color:white' href='./admin_remove.php?poistettava=2'>Delete</a></h2>
                  </td>
                  <td>
                    <h2<a href='./admin_edit.php?editable=2'>Edit</a>
                      </h2>
                  </td>
                </tr>
            </table>
    </div>
    <div class="box" id="a">
      <h1><b>About Us</b></h1>
      <h3>We are a group of students dedicated to sharing our knowledge as we progress with our studies.</h3>
    </div>
    <!--content ends here.-->
  </main>
  <!--Footer starts here.-->

  <footer>
    <section class="mb-4" id="b">
      <!--Section heading-->
      <h2 class="h1-responsive font-weight-bold text-center my-4">Contact Us</h2>
      <!--Section description-->
      <p class="text-center w-responsive mx-auto mb-5">Do you have any questions? Please do not hesitate to contact us directly. Our team will come back to you within a matter of hours to help you.</p>
      <div class="row">
        <div class="col-md-9 mb-md-0 mb-5">
          <form id="contact-form" name="contact-form" action="mail.php" method="POST">
            <div class="row">
              <div class="col-md-6">
                <div class="md-form mb-0">
                  <input type="text" id="name" name="name" class="form-control">
                  <label for="name" class="">Your name</label>
                </div>
              </div>
              <div class="col-md-6">
                <div class="md-form mb-0">
                  <input type="text" id="email" name="email" class="form-control">
                  <label for="email" class="">Your email</label>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-md-12">
                <div class="md-form mb-0">
                  <input type="text" id="subject" name="subject" class="form-control">
                  <label for="subject" class="">Subject</label>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-md-12">
                <div class="md-form">
                  <textarea id="message" name="message" rows="2" class="form-control md-textarea"></textarea>
                  <label for="message">Your message</label>
                </div>
              </div>
            </div>

          </form>
          <div class="text-center text-md-left">
            <a class="btn btn-primary" onclick="document.getElementById('contact-form').submit();">Send</a>
          </div>
          <div class="status"></div>
        </div>
        <div class="col-md-3 text-center">
          <ul class="list-unstyled mb-0">
            <li><i class="fas fa-map-marker-alt fa-2x"></i>
              <p>Visamäentie 35 A, 13100 Hämeenlinna</p>
            </li>
            <li><i class="fas fa-phone mt-4 fa-2x"></i>
              <p>(+358) 23456 789</p>
            </li>
            <li><i class="fas fa-envelope mt-4 fa-2x"></i>
              <p>[email protected]</p>
            </li>
          </ul>
        </div>
      </div>
    </section>
    <div class="container">
      <div class="py-3 my-4">
        <ul class="nav justify-content-center border-bottom pb-3 mb-3">
          <li class="nav-item"><a class="nav-link px-2 text-muted">Facebook</a></li>
          <li class="nav-item"><a class="nav-link px-2 text-muted">Instagram</a></li>
          <li class="nav-item"><a class="nav-link px-2 text-muted">LinkedIn</a></li>
          <li class="nav-item"><a class="nav-link px-2 text-muted">Discord</a></li>
        </ul>
        <p class="text-center text-muted">&copy; 2022 HAMK, Team 14</p>
      </div>
    </div>
  </footer>
  <!--Footer ends here.-->
</body>

</html>

Any help would be much appreciated!

shubham kakade

You are looping the table tag in while loop that's why multiple tables are getting created. Try doing like this

echo "<table>";
    echo "<tr>";
    echo "<th>ID</th>";
    echo "<th>First Name</th>";
    echo "<th>last Name</th>";
    echo "<th>User Name</th>";
    echo "<th>Email</th>";
    echo "<th>Password</th>";
    echo "<th>Description</th>";
    echo "<th>Delete</th>";
    echo "<th>Edit</th>";
    echo "</tr>";
while ($row=mysqli_fetch_object($print)) {
    echo "<tr>";
    echo "<td><h2>$row->fname</h2></td>";
    echo "<td><h2>$row->lname</h2></td>";
    echo "<td><h2>$row->uname</h2></td>";
    echo "<td><h2>$row->email</h2></td>";
    echo "<td><h2>$row->paswd</h2></td>";
    echo "<td><h2>$row->descrip</h2></td>";
    echo "<td><h2><a style='color:white' href='./admin_remove.php?poistettava=$row->id'>Delete</a></h2></td>";
    echo "<td><h2<a href='./admin_edit.php?editable=$row->id'>Edit</a></h2></td>";
    echo "</tr>";
 }
    
echo "</table>";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Display data from database table into html table inside a php file

php code to retrieve data from mysql database and display in html table

Display data in html/js file using NodeJs from mysql database

Display data from mysql database using a table

How to display a link from the MySql database in html using jquery or php

How to display data from database into line graph using php and mysql?

How to show table from MySQL database using php and html

How can I get data from database into HTML table?

How to delete mysql database data from table, using php

How to Insert from HTML/PHP Data into MySQL Database using jQuery

How can I get all my data from a table in my database and show it using php?

How can i delete data from database with foreign key constraint in mysql using php pdo

How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript?

Fetch and display data from database in table format in HTML form using PHP

Display data on html from mysql database using flask

Format using MYSQL Date Retrieved from Database and Display in PHP file

How to display data for a certain period of time (day / week / month) from Mysql table in html table (php)

How to display all table names from particular mysql database in php

i can't display/access images from the mysql database using php

How to Display data from moodle database in an html table

How to get data from database and display to table row span in php

How can I join 2 cells in a table while retrieving data from database from PHP?

How to retrieve data from MySQL database in Django and display it in html page?

make a html from mysql(php) database using pretty table module

How can I display specific data from a MySQL database? (fetching user's email)

How can I create a text input with autocomplete and then display data from mysql database in express?

How to put data from database table to a html table using ajax and php

how to display data from MySQL database in listview in php

How to retrieve image file paths from mysql database and display the images using php