Show data pulled from database, based on html form

2019-02-16 02:27发布

问题:

I have a html page with an input box. I am trying to retrieve data from the database based on that input and send it back to the original html page in the form of a table using php. I created the php in an external file and it was displaying the correct values in the browser but my issue is trying to display the php on the html page.

I enabled my server to parse html files and have put the php within the html file but still was not working. When I entered a value and submit the page reloads but nothing is displayed.

If anyone knows the best way of doing this I would appreciate the advice. I have read numerous threads and none seem to work.

PHP:

<?php

define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
     die('Could not connect: ' . mysqli_connect_error());
     }

$studentnum = $_POST['studentnum'];     

$sql = "SELECT * FROM test WHERE number LIKE '%$studentnum%'"; 
$result=mysqli_query($conn, $sql);

echo '<table class="table table-striped table-bordered table-hover">'; 
echo "<tr>
      <th>Name</th>
      <th>Number</th>
      <th>Floor</th>
      <th>Room</th>
      <th>Message</th>
      </tr>"; 
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
  echo "<tr><td>"; 
  echo $row['name'];
  echo "</td><td>"; 
  echo $row['number'];
  echo "</td><td>";   
  echo $row['floor'];
  echo "</td><td>"; 
  echo $row['room'];
  echo "</td><td>";   
  echo $row['message'];
  echo "</td></tr>";  

}
echo "</table>";  

mysqli_close($conn);
?>

HTML:

<form action="test.php" method="post">
            <label>Enter Student Number:</label>
            <input name="studentnum" type="number" placeholder="Type Here">
                <br>
                <br>
            <input type="submit" value="Enter">

    </form>

回答1:

If you want it all on the same page you can use the below, assuming your parse php in html files is working and your page is index.html (as specified in the form action).

<?php

// check if the form has been submitted and display the results
if (isset($_POST['studentnum'])) {

  define('DB_NAME', 'database');
  define('DB_USER', 'user');
  define('DB_PASSWORD', 'password');
  define('DB_HOST', 'localhost');

  $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$conn) {
    die('Could not connect: ' . mysqli_connect_error());
  }

  // escape the post data to prevent injection attacks
  $studentnum = mysqli_real_escape_string($conn, $_POST['studentnum']);

  $sql = "SELECT * FROM `test` WHERE `number` LIKE '%$studentnum%'"; 
  $result=mysqli_query($conn, $sql);

  // check if the query returned a result
  if (!$result) {
      echo 'There are no results for your search';
  } else {
    // result to output the table
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr>
          <th>Name</th>
          <th>Number</th>
          <th>Floor</th>
          <th>Room</th>
          <th>Message</th>
          </tr>"; 
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
      echo "<tr><td>"; 
      echo $row['name'];
      echo "</td><td>"; 
      echo $row['number'];
      echo "</td><td>";   
      echo $row['floor'];
      echo "</td><td>"; 
      echo $row['room'];
      echo "</td><td>";   
      echo $row['message'];
      echo "</td></tr>";  
    }
    echo "</table>";
  }

  mysqli_close($conn);
} // end submitted
else
{
// not submitted to output the form
?>
<form action="index.html" method="post">
  <label>Enter Student Number:</label>
  <input name="studentnum" type="number" placeholder="Type Here">
  <br>
  <br>
  <input type="submit" value="Enter">
</form>
<?php
} // end not submitted
?>