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 us
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
?>