How to get data from MySQL database with PHP and put it in an HTML table – Code Explained

Here’s the php code I used to output from my database. You’ll need to put in the proper connections to change everything over to work for your app.

<table class="table table-striped table-responsive-md btn-table">
<!-- -->
  <thead>
    <tr>


      <th>Username</th>
      <th>Current Game</th>
      <th>In-Game Username</th>
      <th>Looking for</th>
      <th>Reputation Score</th>
      <th>Contact via</th>
    </tr>
  </thead>
<!-- -->
  <tbody>
    <!-- -->
    <tr>

      <?php
      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "gamerapp";

      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      }

      $sql = "SELECT username FROM userdata";
      $result = $conn->query($sql);

      if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
              echo "<tr><td> " . $row["username"]. "</td></tr>";
          }
      } else {
          echo "0 results";
      }
      $conn->close();
      ?>
  </tbody>

</table>

How to make a signup form in PHP that posts data to MYSQLI database – Code Explained

Here’s the code for “firststepssignup.php”

<form action="insertfirstinfo.php" method="POST" role="form">

  <div class="form-group">

    <div class="text-center">
      <h1 class="display-4 p-2" >Signup</h1>
    </div>

    <!-- here is where the first and last name are collected and sent to the database -->

    <div class="form-group">
      <div class="row">
        <!-- here is where the first and last name are collected and sent to the database -->

        <div class="col">
          <label for="firstname">First Name </label>
          <input id="first_name" name="first_name" type="text" class="form-control" placeholder="First name" required>
        </div>

        <div class="col">
          <label for="lastname">Last Name </label>
          <input id="lastname" name="lastname" type="text" class="form-control" placeholder="Last name">
        </div>
      </div>
    </div>

    <div class="form-group">
      <!-- here's where the email address is collected and sent to the database -->
      <label for="email">Email address</label>

      <input id="email" name="email" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">

    </div>

    <div class="form-group">
        <!-- here is where the username is collected and sent to the datbase -->
      <label for="username">Username</label>

      <input id="username" name="username" type="username" class="form-control" id="usernameInput" placeholder="Username">

    </div>

    <div class="form-group">
        <!-- -->
      <label for="password">Password</label>

      <input id="password" name="password" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">

    </div>
    <!-- -->

  <button href="index.php" type="submit" value="submit" class="btn btn-primary btn-block">
    <a style="color: white;" href="index.php">Next</a>
  </button>

</form>

Here’s the code for the action of the form in “firststepssignup.php”

YOU WILL NEED TO HAVE AN INDEX.PHP FILE THAT BRINGS THIS ALL TOGETHER AND INCLUDES BOOTSTRAP OR THIS CODE WILL LOOK HORRIBLE.

Here’s “insertfirstinfo.php”

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "gamerapp");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);
$lastname = mysqli_real_escape_string($link, $_REQUEST['lastname']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$password = mysqli_real_escape_string($link, $_REQUEST['password']);
$username = mysqli_real_escape_string($link, $_REQUEST['username']);


// Attempt insert query execution
$sql = "INSERT INTO userdata (first_name, lastname, email, password, username) VALUES ('$first_name', '$lastname', '$email', '$password', '$username')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
    header('location: index.php');
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>